Class: Pgbus::RecurringTask

Inherits:
BusRecord
  • Object
show all
Defined in:
app/models/pgbus/recurring_task.rb

Class Method Summary collapse

Class Method Details

.sync_from_config!(tasks_hash) ⇒ Object

Sync static tasks from configuration. Creates new tasks, updates existing ones, removes stale ones.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/pgbus/recurring_task.rb', line 16

def self.sync_from_config!(tasks_hash)
  transaction do
    task_keys = tasks_hash.keys

    # Upsert all configured tasks
    tasks_hash.each do |key, options|
      options = options.transform_keys(&:to_s)
      record = find_or_initialize_by(key: key)
      record.assign_attributes(
        class_name: options["class"],
        command: options["command"],
        schedule: options["schedule"],
        queue_name: options["queue"],
        arguments: options["args"],
        priority: options.fetch("priority", 0).to_i,
        description: options["description"],
        static: true
      )
      record.save!
    end

    # Remove static tasks no longer in config
    static_tasks.where.not(key: task_keys).delete_all
  end
end