Class: Jobit::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/jobit/job.rb

Class Method Summary collapse

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

  options = {}
  options = yield if block_given?

  options[:name] = name
  options[:object] = object
  options[:args] = Marshal.dump(args)

  Jobby.new(nil, options)
end

.allObject

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_allObject



20
21
22
# File 'lib/jobit/job.rb', line 20

def self.destroy_all
  Storage.destroy_all
end

.destroy_failedObject



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_pathObject



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

.loggerObject



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_nameObject



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