Class: JobRunner

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/job_runner.rb

Defined Under Namespace

Classes: BackgroundJobError, JobRunnerError, JobRunnerNotFound, RegisteredRunner

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ JobRunner

Returns a new instance of JobRunner.



94
95
96
97
98
99
# File 'backend/app/lib/job_runner.rb', line 94

def initialize(job)
  @job = job
  RequestContext.open(:repo_id => @job.repo_id) do
    @json = Job.to_jsonmodel(job)
  end
end

Class Method Details

.for(job) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'backend/app/lib/job_runner.rb', line 73

def self.for(job)
  type = ASUtils.json_parse(job[:job_blob])['jsonmodel_type']

  unless @@runners.has_key?(type)
    raise JobRunnerNotFound.new("No suitable runner found for #{type}")
  end

  @@runners[type].runner.new(job)
end

.register_for_job_type(type, opts = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'backend/app/lib/job_runner.rb', line 57

def self.register_for_job_type(type, opts = {})
  @@runners ||= {}
  if !opts.fetch(:allow_reregister, false) && @@runners.has_key?(type)
    raise JobRunnerError.new("Attempting to register #{self} for job type #{type} " +
                             "- already handled by #{@@runners[type]}")
  end

  @@runners[type] = RegisteredRunner.new(type,
                                         self,
                                         opts.fetch(:hidden, false),
                                         opts.fetch(:run_concurrently, false),
                                         [opts.fetch(:create_permissions, [])].flatten,
                                         [opts.fetch(:cancel_permissions, [])].flatten)
end

.registered_job_typesObject



89
90
91
92
# File 'backend/app/lib/job_runner.rb', line 89

def self.registered_job_types
  Hash[ @@runners.reject {|k, v| v[:hidden] }.map { |k, v| [k, {:create_permissions => v.create_permissions,
                                                              :cancel_permissions => v.cancel_permissions}] } ]
end

.registered_runner_for(type) ⇒ Object



84
85
86
# File 'backend/app/lib/job_runner.rb', line 84

def self.registered_runner_for(type)
  @@runners.fetch(type) { raise NotFoundException.new("No runner found for #{type}") }
end

Instance Method Details

#add_success_hook(&block) ⇒ Object



102
103
104
105
# File 'backend/app/lib/job_runner.rb', line 102

def add_success_hook(&block)
  @success_hooks ||= []
  @success_hooks << block
end

#cancelation_signaler(canceled) ⇒ Object



119
120
121
# File 'backend/app/lib/job_runner.rb', line 119

def cancelation_signaler(canceled)
  @job_canceled = canceled
end

#canceled?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'backend/app/lib/job_runner.rb', line 115

def canceled?
  @job_canceled.value
end

#parse_job_params_string(job_params) ⇒ Object



123
124
125
126
127
128
# File 'backend/app/lib/job_runner.rb', line 123

def parse_job_params_string(job_params)
  param_string = job_params[1..-2].delete('\\\\')
  params = ASUtils.json_parse(param_string)
  params = symbol_keys(params)
  params
end

#runObject

Implement this in your subclass

This is the method that does the actual work

Raises:



49
50
51
# File 'backend/app/lib/job_runner.rb', line 49

def run
  raise JobRunnerError.new("#{self.class} must implement the #run method")
end

#success!Object



108
109
110
111
112
# File 'backend/app/lib/job_runner.rb', line 108

def success!
  Array(@success_hooks).each do |hook|
    hook.call
  end
end

#symbol_keys(hash) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'backend/app/lib/job_runner.rb', line 130

def symbol_keys(hash)
  h = hash.map do |k, v|
    v_sym = if v.instance_of? Hash
              v = symbol_keys(v)
            else
              v
            end

    [k.to_sym, v_sym]
  end
  Hash[h]
end