Class: SimpleWorker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_worker/base.rb,
lib/simple_worker/server/overrides.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.caller_fileObject

Returns the value of attribute caller_file.



12
13
14
# File 'lib/simple_worker/base.rb', line 12

def caller_file
  @caller_file
end

.subclassObject

Returns the value of attribute subclass.



12
13
14
# File 'lib/simple_worker/base.rb', line 12

def subclass
  @subclass
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/simple_worker/base.rb', line 9

def response
  @response
end

#schedule_idObject

Returns the value of attribute schedule_id.



8
9
10
# File 'lib/simple_worker/base.rb', line 8

def schedule_id
  @schedule_id
end

#task_idObject

Returns the value of attribute task_id.



8
9
10
# File 'lib/simple_worker/base.rb', line 8

def task_id
  @task_id
end

#task_set_idObject

Returns the value of attribute task_set_id.



8
9
10
# File 'lib/simple_worker/base.rb', line 8

def task_set_id
  @task_set_id
end

Class Method Details

.inherited(subclass) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/simple_worker/base.rb', line 31

def inherited(subclass)
  subclass.reset!

  caller_file = caller[0][0...(caller[0].index(":in"))]
  caller_file = caller_file[0...(caller_file.rindex(":"))]
  subclass.instance_variable_set(:@caller_file, caller_file)

  super
end

.merge(*files) ⇒ Object

merges the specified file.

Example: merge ‘models/my_model’



101
102
103
104
105
106
107
# File 'lib/simple_worker/base.rb', line 101

def merge(f)
  f2 = SimpleWorker::MergeHelper.check_for_file(f, @caller_file)
  fbase = f2[:basename]
  ret = f2
  @merged[fbase] = ret
  ret
end

.merge_folder(path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/simple_worker/base.rb', line 76

def merge_folder(path)
  files = []
  #puts "caller_file=" + caller_file
  if path[0, 1] == '/'
    abs_dir = path
  else # relative
    abs_dir = File.join(File.dirname(caller_file), path)
  end
  #puts 'abs_dir=' + abs_dir
  raise "Folder not found for merge_folder #{path}!" unless File.directory?(abs_dir)
  rbfiles = File.join(abs_dir, "*.rb")
  Dir[rbfiles].each do |f|
    #f2 = check_for_file(f)
    #puts "f2=#{f2}"
    merge(f)
    #files << f
    #@merged[f]
  end
  #@merged_folders[path] = files unless files.empty?
  #SimpleWorker.logger.info "Merged folders! #{@merged_folders.inspect}"
end

.merge_gem(gem, version = nil) ⇒ Object

merges the specified gem.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simple_worker/base.rb', line 42

def merge_gem(gem_name, options={})
  gem_info = SimpleWorker::MergeHelper.create_gem_info(gem_name, options)
  @merged_gems[gem_name.to_s] = gem_info
  reqs = gem_info[:require].is_a?(Array) ? gem_info[:require] : [gem_info[:require]]
  reqs.each do |r|
    r2 = "#{gem_info[:path]}/lib/#{r}"
    begin
      SimpleWorker.logger.debug 'requiring ' + r2
      require r2
    rescue LoadError=>ex
      SimpleWorker.logger.error "Error requiring gem #{r}: #{ex.message}"
      raise "Gem #{gem_name} was found, but we could not load the file '#{r2}'. You may need to use :require=>x.........."
    end
  end
end

.merge_mailer(mailer, params = nil) ⇒ Object

merge action_mailer mailers



69
70
71
72
73
74
# File 'lib/simple_worker/base.rb', line 69

def merge_mailer(mailer, params={})
  f2 = SimpleWorker::MergeHelper.check_for_file(mailer, @caller_file)
  basename = File.basename(mailer, f2[:extname])
  path_to_templates = params[:path_to_templates] || File.join(Rails.root, "app/views/#{basename}")
  @merged_mailers[basename] = {:name=>basename, :path_to_templates=>path_to_templates, :filename => f2[:path]}.merge!(params)
end

.merge_worker(file, class_name) ⇒ Object

Use this to merge in other workers. These are treated differently the normal merged files because they will be uploaded separately and treated as distinctly separate workers.

file: This is the path to the file, just like merge. class_name: eg: ‘MyWorker’.



124
125
126
127
128
129
130
131
# File 'lib/simple_worker/base.rb', line 124

def merge_worker(file, class_name)
#        puts 'merge_worker in ' + self.name
  ret = merge(file)
  ret[:class_name] = class_name
  #[File.expand_path(file), class_name]
  @merged_workers[file] = ret
  ret
end

.reset!Object



21
22
23
24
25
26
27
28
29
# File 'lib/simple_worker/base.rb', line 21

def reset!
  @merged = {}
  @merged_workers = {}
  @merged_gems = {}
  @merged_mailers = {}
  @merged_folders = {}
  @unmerged = {}
  @unmerged_gems = {}
end

.unmerge(*files) ⇒ Object

Opposite of merge, this will omit the files you specify from being merged in. Useful in Rails apps where a lot of things are auto-merged by default like your models.



111
112
113
114
115
116
# File 'lib/simple_worker/base.rb', line 111

def unmerge(f)
  f2 = SimpleWorker::MergeHelper.check_for_file(f, @caller_file)
  fbase = f2[:basename]
  @unmerged[fbase] = f2
  @merged.delete(fbase)
end

.unmerge_gem(gem_name) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/simple_worker/base.rb', line 59

def unmerge_gem(gem_name)
  #gem_info = {:name=>gem_name}
  #@unmerged_gems[gem_name.to_s] = gem_info
  gs = gem_name.to_s
  gem_info = {:name=>gs}
  @unmerged_gems[gs] = gem_info
  @merged_gems.delete(gs)
end

Instance Method Details

#after_runObject



306
307
308
# File 'lib/simple_worker/base.rb', line 306

def after_run

end

#after_uploadObject



298
299
300
# File 'lib/simple_worker/base.rb', line 298

def after_upload

end

#before_runObject



302
303
304
# File 'lib/simple_worker/base.rb', line 302

def before_run

end

#before_uploadObject

Callbacks for developer



294
295
296
# File 'lib/simple_worker/base.rb', line 294

def before_upload

end

#enqueue(options = {}) ⇒ Object



206
207
208
# File 'lib/simple_worker/base.rb', line 206

def enqueue(options={})
  queue(options)
end

#get_log(options = {}) ⇒ Object

Retrieves the log for this worker from the SimpleWorker service.



289
290
291
# File 'lib/simple_worker/base.rb', line 289

def get_log(options={})
  SimpleWorker.service.get_log(task_id, options)
end

#init_databaseObject



180
181
182
183
184
185
186
187
# File 'lib/simple_worker/base.rb', line 180

def init_database
  if SimpleWorker.config.database
    require 'active_record'
    if !ActiveRecord::Base.connected?
      ActiveRecord::Base.establish_connection(SimpleWorker.config.database)
    end
  end
end

#init_mailerObject



172
173
174
175
176
177
178
# File 'lib/simple_worker/base.rb', line 172

def init_mailer
  if SimpleWorker.config.mailer
    require 'action_mailer'
    ActionMailer::Base.raise_delivery_errors = true
    ActionMailer::Base.smtp_settings = (SimpleWorker.config.mailer)
  end
end

#is_local?Boolean

:todo remove this method later, when new simple_worker gem will be released

Returns:

  • (Boolean)


98
99
100
# File 'lib/simple_worker/server/overrides.rb', line 98

def is_local?
  !is_remote?
end

#is_remote?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/simple_worker/base.rb', line 247

def is_remote?
  false
end

#log(str) ⇒ Object



135
136
137
# File 'lib/simple_worker/base.rb', line 135

def log(str)
  puts str.to_s
end

#orig_queueObject

Call this to queue up your job to SimpleWorker cloud. options:

:priority => 0, 1 or 2. Default is 0.
:recursive => true/false. Default is false. If you queue up a worker that is the same class as the currently
              running worker, it will be rejected unless you set this explicitly so we know you meant to do it.


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/simple_worker/server/overrides.rb', line 138

def queue(options={})
#            puts 'in queue'
  set_auto_attributes
  upload_if_needed(options)

  response = SimpleWorker.service.queue(self.class.name, sw_get_data, options)
  SimpleWorker.service.logger.debug 'queue response=' + response.inspect
  @response = response
  @task_id = response["tasks"][0]["id"]
  response
end

#orig_scheduleObject

schedule: hash of scheduling options that can include:

Required:
- start_at:      Time of first run - DateTime or Time object.
Optional:
- run_every:     Time in seconds between runs. If ommitted, task will only run once.
- delay_type:    Fixed Rate or Fixed Delay. Default is fixed_delay.
- end_at:        Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
- run_times:     Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
- name:          Provide a name for the schedule, defaults to class name. Use this if you want more than one schedule per worker class.


139
140
141
142
143
144
145
146
147
# File 'lib/simple_worker/server/overrides.rb', line 139

def schedule(schedule)
  set_global_attributes
  upload_if_needed(schedule)

  response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
  SimpleWorker.service.logger.debug 'schedule response=' + response.inspect
  @schedule_id = response["schedules"][0]["id"]
  response
end

#orig_statusObject

Receive the status of your worker.



140
141
142
143
144
145
146
147
148
149
# File 'lib/simple_worker/server/overrides.rb', line 140

def status
  check_service
  if task_id
    task_status
  elsif schedule_id
    schedule_status
  else
    raise "Queue or schedule before check status."
  end
end

#queue(options = {}) ⇒ Object

Call this to queue up your job to SimpleWorker cloud. options:

:priority => 0, 1 or 2. Default is 0.
:recursive => true/false. Default is false. If you queue up a worker that is the same class as the currently
              running worker, it will be rejected unless you set this explicitly so we know you meant to do it.


215
216
217
218
219
220
221
222
223
224
225
# File 'lib/simple_worker/base.rb', line 215

def queue(options={})
#            puts 'in queue'
  set_auto_attributes
  upload_if_needed(options)

  response = SimpleWorker.service.queue(self.class.name, sw_get_data, options)
  SimpleWorker.service.logger.debug 'queue response=' + response.inspect
  @response = response
  @task_id = response["tasks"][0]["id"]
  response
end

#run_localObject

Call this if you want to run locally and get some extra features from this gem like global attributes.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/simple_worker/base.rb', line 156

def run_local
  # puts 'run_local'
  set_auto_attributes
  init_database
  init_mailer
  begin
    run
  rescue => ex
    if self.respond_to?(:rescue_all)
      rescue_all(ex)
    else
      raise ex
    end
  end
end

#same_clazz?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/simple_worker/server/overrides.rb', line 171

def same_clazz?
  SimpleWorker.running_class == self.class
end

#schedule(schedule) ⇒ Object

schedule: hash of scheduling options that can include:

Required:
- start_at:      Time of first run - DateTime or Time object.
Optional:
- run_every:     Time in seconds between runs. If ommitted, task will only run once.
- delay_type:    Fixed Rate or Fixed Delay. Default is fixed_delay.
- end_at:        Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
- run_times:     Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
- name:          Provide a name for the schedule, defaults to class name. Use this if you want more than one schedule per worker class.


274
275
276
277
278
279
280
281
282
# File 'lib/simple_worker/base.rb', line 274

def schedule(schedule)
  set_global_attributes
  upload_if_needed(schedule)

  response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
  SimpleWorker.service.logger.debug 'schedule response=' + response.inspect
  @schedule_id = response["schedules"][0]["id"]
  response
end

#schedule_statusObject



284
285
286
# File 'lib/simple_worker/base.rb', line 284

def schedule_status
  SimpleWorker.service.schedule_status(schedule_id)
end

#set_auto_attributesObject



189
190
191
# File 'lib/simple_worker/base.rb', line 189

def set_auto_attributes
  set_global_attributes
end

#set_global_attributesObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/simple_worker/base.rb', line 193

def set_global_attributes
  return unless SimpleWorker.config
  ga = SimpleWorker.config.global_attributes
  if ga && ga.size > 0
    ga.each_pair do |k, v|
#                    puts "k=#{k} v=#{v}"
      if self.respond_to?(k)
        self.send("#{k}=", v)
      end
    end
  end
end

#set_progress(hash) ⇒ Object



143
144
145
# File 'lib/simple_worker/base.rb', line 143

def set_progress(hash)
  puts 'set_progress: ' + hash.inspect
end

#somethingObject



111
112
113
# File 'lib/simple_worker/server/overrides.rb', line 111

def something
  puts 'which class? ' + self.class.name
end

#statusObject

Receive the status of your worker.



228
229
230
231
232
233
234
235
236
237
# File 'lib/simple_worker/base.rb', line 228

def status
  check_service
  if task_id
    task_status
  elsif schedule_id
    schedule_status
  else
    raise "Queue or schedule before check status."
  end
end

#sw_set_data(data) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/simple_worker/server/overrides.rb', line 120

def sw_set_data(data)
  if data["attr_encoded"]
    # new way, attributes are base 64 encoded
    data = JSON.parse(Base64.decode64(data["attr_encoded"]))
  end

  data.each_pair do |k, v|
    next unless k[0] == "@"
#        puts "setting instance_variable #{k}=#{v}"
    self.instance_variable_set(k, v)
  end

end

#task_statusObject



239
240
241
# File 'lib/simple_worker/base.rb', line 239

def task_status
  SimpleWorker.service.status(task_id)
end

#uploadObject



259
260
261
# File 'lib/simple_worker/base.rb', line 259

def upload
  upload_if_needed
end

#upload_if_needed(options = {}) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/simple_worker/base.rb', line 364

def upload_if_needed(options={})
  check_service
  SimpleWorker.service.check_config

  before_upload

  merged = self.class.instance_variable_get(:@merged)

  # do merged_workers first because we need to get their subclasses and what not too
  merged_workers = self.class.instance_variable_get(:@merged_workers)
  if merged_workers && merged_workers.size > 0
    SimpleWorker.logger.debug 'now uploading merged workers ' + merged_workers.inspect
    merged_workers.each_pair do |mw, v|
      SimpleWorker.logger.debug 'Instantiating and uploading ' + v.inspect
      mw_instantiated = Kernel.const_get(v[:class_name]).new
      mw_instantiated.upload

      merged, rfile, subclass = SimpleWorker::Base.extract_superclasses_merges(mw_instantiated, merged)
      merged.merge!(SimpleWorker::Base.extract_merged_workers(mw_instantiated))

    end
  end

  if !uploaded?
    unmerged = self.class.instance_variable_get(:@unmerged)
    merged_gems = self.class.instance_variable_get(:@merged_gems)
    merged_mailers = self.class.instance_variable_get(:@merged_mailers)
    merged_folders = self.class.instance_variable_get(:@merged_folders)
    merged, rfile, subclass = SimpleWorker::Base.extract_superclasses_merges(self, merged)
    merged_mailers = merged_mailers.merge(SimpleWorker.config.mailers) if SimpleWorker.config.mailers
    unless merged_gems.size == 0
      merged_gems = gems_to_merge(merged_gems)
    end

    options_for_upload = {:merge=>merged, :unmerge=>unmerged, :merged_gems=>merged_gems, :merged_mailers=>merged_mailers, :merged_folders=>merged_folders}
    options_for_upload.merge!(options)
    SimpleWorker.service.upload(rfile, subclass.name, options_for_upload)
    self.class.instance_variable_set(:@uploaded, true)
  else
    SimpleWorker.logger.debug 'Already uploaded for ' + self.class.name
  end

  after_upload
end

#uploaded?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/simple_worker/base.rb', line 151

def uploaded?
  self.class.instance_variable_defined?(:@uploaded) && self.class.instance_variable_get(:@uploaded)
end

#user_dirObject



139
140
141
# File 'lib/simple_worker/base.rb', line 139

def user_dir
  "./"
end

#wait_until_completeObject

will return after job has completed or errored out. Returns status. todo: add a :timeout option



254
255
256
257
# File 'lib/simple_worker/base.rb', line 254

def wait_until_complete
  check_service
  SimpleWorker.service.wait_until_complete(self.task_id)
end

#who_am_i?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/simple_worker/base.rb', line 147

def who_am_i?
  return self.class.name
end