Class: SolidObjects::ProcessPruner

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/process_pruner.rb,
sig/generated/lib/solid_objects/process_pruner.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(now: SolidObjects.database_adapter.database_now, batch_size: SolidObjects.configuration.prune_batch_size) ⇒ ProcessPruner

Returns a new instance of ProcessPruner.

RBS:

  • (?now: Time, ?batch_size: Integer) -> void

Parameters:

  • now: (Time) (defaults to: SolidObjects.database_adapter.database_now)
  • batch_size: (Integer) (defaults to: SolidObjects.configuration.prune_batch_size)


9
10
11
12
13
14
15
# File 'lib/solid_objects/process_pruner.rb', line 9

def initialize(
  now: SolidObjects.database_adapter.database_now,
  batch_size: SolidObjects.configuration.prune_batch_size
)
  @now = now
  @batch_size = batch_size
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.

Returns:

  • (Object)


39
40
41
# File 'lib/solid_objects/process_pruner.rb', line 39

def batch_size
  @batch_size
end

#nowObject (readonly)

Returns the value of attribute now.

Returns:

  • (Object)


39
40
41
# File 'lib/solid_objects/process_pruner.rb', line 39

def now
  @now
end

Instance Method Details

#previewInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


18
19
20
# File 'lib/solid_objects/process_pruner.rb', line 18

def preview
  prunable.count
end

#prunableActiveRecord::Relation[Process]

RBS:

  • () -> ActiveRecord::Relation[Process]

Returns:



42
43
44
45
46
47
# File 'lib/solid_objects/process_pruner.rb', line 42

def prunable
  Process.where(
    shutdown_state: "stopped",
    stopped_at: ...(now - SolidObjects.configuration.process_retention)
  )
end

#pruneInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solid_objects/process_pruner.rb', line 23

def prune
  deleted = 0

  loop do
    process_ids = prunable.limit(batch_size).pluck(:id)
    break if process_ids.empty?

    deleted += Process.where(id: process_ids).delete_all
  end

  SolidObjects.instrument(:"processes.pruned", count: deleted)
  deleted
end