Class: Workflow::Join::Sidekiq::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Workflow
Defined in:
lib/workflow/join/sidekiq/job.rb

Defined Under Namespace

Classes: Worker

Constant Summary collapse

DONE =

attr_accessor :worker, :args, :result, :fails

:done
TABLE_NAME =
'workflow_jobs'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup!(host, worker) ⇒ Object



40
41
42
43
# File 'lib/workflow/join/sidekiq/job.rb', line 40

def lookup!(host, worker)
  params = { host: host.class.to_s, host_id: host.id, worker: worker.to_s }
  where(**params).last || create!(**params)
end

.migration(table_name = TABLE_NAME) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/workflow/join/sidekiq/job.rb', line 12

def migration(table_name = TABLE_NAME)
  <<-MIGRATION
  class CreateWorkflowJobs < ::ActiveRecord::Migration
    def change
      create_table :#{table_name} do |t|
        t.string   :workflow_state, default: 'scheduled', null: false

        t.string   :host
        t.integer  :host_id
        t.string   :worker

        t.text     :args
        t.text     :result
        t.text     :fails

        t.string   :workflow_pending_transitions
        t.string   :workflow_pending_callbacks

        t.timestamps
      end

      add_index :#{table_name}, :workflow_state, unique: false
      add_index :#{table_name}, [:host, :host_id, :worker], unique: true
    end
  end
  MIGRATION
end

.worker(worker) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/workflow/join/sidekiq/job.rb', line 45

def worker(worker)
  case worker
  when String, Symbol then Kernel.const_get(worker)
  when Module then worker
  else fail ArgumentError, "Workflow::Join::Sidekiq::Job#worker expects a string/class as an argument, got #{worker.inspect}."
  end
end

Instance Method Details

#on_failed_entry(_old_state, _event, *args) ⇒ Object



78
79
80
81
# File 'lib/workflow/join/sidekiq/job.rb', line 78

def on_failed_entry(_old_state, _event, *args)
  timestamp = Time.respond_to?(:zone) && Time.zone ? Time.zone.now : Time.now
  (self.fails ||= {})[timestamp] = args
end

#on_running_entry(_old_state, _event, *args) ⇒ Object



74
75
76
# File 'lib/workflow/join/sidekiq/job.rb', line 74

def on_running_entry(_old_state, _event, *args)
  Job::Worker.perform_async(*args, ★: id) # FIXME: Anything more elegant?
end

#to_hashObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/workflow/join/sidekiq/job.rb', line 89

def to_hash
  {
    # id: id,
    host: host,
    host_id: host_id,
    worker: worker,
    args: args,
    result: result,
    errors: fails,
    workflow_state: workflow_state,
    state: workflow_state.to_sym
  }
end