Module: Rukawa::Wrapper::ActiveJob

Defined in:
lib/rukawa/wrapper/active_job.rb

Class Method Summary collapse

Class Method Details

.[](job_class) ⇒ Object



8
9
10
11
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
39
40
41
42
43
44
45
# File 'lib/rukawa/wrapper/active_job.rb', line 8

def self.[](job_class)
  raise "Please set Rukawa.config.status_store subclass of ActiveSupport::Cache::Store" unless Rukawa.config.status_store
  @wrapper_classes ||= {}
  return @wrapper_classes[job_class] if @wrapper_classes[job_class]

  wrapper = Class.new(Rukawa::Job) do
    set_resource_count 0

    define_singleton_method(:origin_class) do
      job_class
    end

    def initialize(variables: {}, context: Context.new, parent_job_net: nil)
      super
      @job_class = self.class.origin_class
    end

    def run
      @job_class.include(Hooks) unless @job_class.include?(Hooks)
      @job_class.prepend(HooksForFailure) unless @job_class.include?(HooksForFailure)
      job = @job_class.perform_later

      status_store = Rukawa::Remote::StatusStore.new(job_id: job.job_id)
      finish_statuses = [Rukawa::Remote::StatusStore::COMPLETED, Rukawa::Remote::StatusStore::FAILED]
      until finish_statuses.include?(last_status = status_store.fetch)
        sleep 0.1
      end

      status_store.delete

      raise WrappedJobError if last_status == Rukawa::Remote::StatusStore::FAILED
    end
  end

  @wrapper_classes[job_class] = wrapper
  Rukawa::Wrapper.const_set("#{job_class.to_s.gsub(/::/, "_")}Wrapper", wrapper)
  wrapper
end