Class: Jobit::Job
- Inherits:
-
Object
- Object
- Jobit::Job
- Defined in:
- lib/jobit/job.rb
Class Method Summary collapse
-
.add(name, object, *args, &block) ⇒ Object
Jobit::Job.add(name, object, *args) {options} options: :priority => the job priority (Integer) :run_at => run job at some time ex: :run_at => Time.now + 4.hours :schedule => run job at some time.
-
.all ⇒ Object
Jobit::Job.all returns array of all jobs.
- .destroy_all ⇒ Object
- .destroy_failed ⇒ Object
-
.find(id) ⇒ Object
Jobit::Job.find(11111.111) returns job or nil.
-
.find_by_name(name) ⇒ Object
Jobit::Job.find_by_name(‘name’) returns job or nil.
- .jobs_path ⇒ Object
- .logger ⇒ Object
-
.where(search) ⇒ Object
Jobit::Job.where(=> ‘name’) returns array of found jobs.
- .work_off(num = 100) ⇒ Object
- .worker_name ⇒ Object
Class Method Details
.add(name, object, *args, &block) ⇒ Object
Jobit::Job.add(name, object, *args) {options} options:
:priority => the job priority (Integer)
:run_at => run job at some time ex: :run_at => Time.now + 4.hours
:schedule => run job at some time. ex: :schedule_at => "16:00"
:repeat => how many times to repeat the job (Integer)
:repeat_delay => delay in seconds before next repeat
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jobit/job.rb', line 71 def self.add(name, object, *args, &block) unless JobitItems.method_defined?("#{object}_task") raise ArgumentError, "Can't add job #{object}. It's not defined in jobs." end = {} = yield if block_given? [:name] = name [:object] = object [:args] = Marshal.dump(args) Jobby.new(nil, ) end |
.all ⇒ Object
Jobit::Job.all returns array of all jobs
42 43 44 |
# File 'lib/jobit/job.rb', line 42 def self.all Storage.all end |
.destroy_all ⇒ Object
20 21 22 |
# File 'lib/jobit/job.rb', line 20 def self.destroy_all Storage.destroy_all end |
.destroy_failed ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/jobit/job.rb', line 24 def self.destroy_failed failed_jobs = Storage.where({:status => 'failed'}) for job in failed_jobs job.destroy end true end |
.find(id) ⇒ Object
Jobit::Job.find(11111.111) returns job or nil
48 49 50 |
# File 'lib/jobit/job.rb', line 48 def self.find(id) Storage.find(id) end |
.find_by_name(name) ⇒ Object
Jobit::Job.find_by_name(‘name’) returns job or nil
54 55 56 |
# File 'lib/jobit/job.rb', line 54 def self.find_by_name(name) Storage.find_by_name(name) end |
.jobs_path ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/jobit/job.rb', line 8 def self.jobs_path if defined?(Rails) "#{Rails.root.to_s}/tmp/jobit" else "/tmp/jobit" end end |
.logger ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/jobit/job.rb', line 32 def self.logger if defined?(Rails) Logger.new("#{Rails.root.to_s}/log/jobit.log", shift_age = 7, shift_size = 1048576) else Logger.new('/dev/null') end end |
.where(search) ⇒ Object
Jobit::Job.where(=> ‘name’) returns array of found jobs
60 61 62 |
# File 'lib/jobit/job.rb', line 60 def self.where(search) Storage.where(search) end |
.work_off(num = 100) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jobit/job.rb', line 86 def self.work_off(num = 100) complete, failed, reissued = 0, 0, 0 num.times do jobs = self.where({:status => 'new'}) break unless jobs.size > 0 job = jobs.first next unless job.time_to_run? res = jobs.first.run_job(self.worker_name) next if res.nil? complete += res[0] failed += res[1] reissued += res[2] job = nil break if $exit end [complete, failed, reissued] end |
.worker_name ⇒ Object
16 17 18 |
# File 'lib/jobit/job.rb', line 16 def self.worker_name "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}" end |