navvy-sequelhooks 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +3 -0
- data/README.textile +15 -0
- data/generators/navvy/navvy_generator.rb +20 -0
- data/generators/navvy/templates/active_record_migration.rb +22 -0
- data/generators/navvy/templates/sequel_migration.rb +23 -0
- data/lib/generators/navvy_generator.rb +32 -0
- data/lib/navvy.rb +34 -0
- data/lib/navvy/configuration.rb +13 -0
- data/lib/navvy/job.rb +173 -0
- data/lib/navvy/job/active_record.rb +136 -0
- data/lib/navvy/job/data_mapper.rb +154 -0
- data/lib/navvy/job/mongo_mapper.rb +150 -0
- data/lib/navvy/job/mongoid.rb +139 -0
- data/lib/navvy/job/sequel.rb +138 -0
- data/lib/navvy/job/sequelhooks.rb +144 -0
- data/lib/navvy/logger.rb +38 -0
- data/lib/navvy/tasks.rb +16 -0
- data/lib/navvy/worker.rb +50 -0
- data/spec/configuration_spec.rb +68 -0
- data/spec/job_spec.rb +505 -0
- data/spec/logger_spec.rb +23 -0
- data/spec/setup/active_record.rb +20 -0
- data/spec/setup/data_mapper.rb +4 -0
- data/spec/setup/mongo_mapper.rb +2 -0
- data/spec/setup/mongoid.rb +9 -0
- data/spec/setup/sequel.rb +21 -0
- data/spec/setup/sequelhooks.rb +21 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/worker_spec.rb +27 -0
- data/spec/zhooks.rb +47 -0
- metadata +160 -0
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'dm-migrations'
|
3
|
+
|
4
|
+
module Navvy
|
5
|
+
class Job
|
6
|
+
include DataMapper::Resource
|
7
|
+
|
8
|
+
property :id, Serial
|
9
|
+
property :object, String
|
10
|
+
property :method_name, String
|
11
|
+
property :arguments, String
|
12
|
+
property :priority, Integer, :default => 0
|
13
|
+
property :return, String
|
14
|
+
property :exception, String
|
15
|
+
property :parent_id, Integer
|
16
|
+
property :created_at, Time
|
17
|
+
property :run_at, Time
|
18
|
+
property :started_at, Time
|
19
|
+
property :completed_at, Time
|
20
|
+
property :failed_at, Time
|
21
|
+
|
22
|
+
##
|
23
|
+
# Add a job to the job queue.
|
24
|
+
#
|
25
|
+
# @param [Object] object the object you want to run a method from
|
26
|
+
# @param [Symbol, String] method_name the name of the method you want to
|
27
|
+
# run
|
28
|
+
# @param [*] arguments optional arguments you want to pass to the method
|
29
|
+
#
|
30
|
+
# @return [Job, false] created Job or false if failed
|
31
|
+
|
32
|
+
def self.enqueue(object, method_name, *args)
|
33
|
+
options = {}
|
34
|
+
if args.last.is_a?(Hash)
|
35
|
+
options = args.last.delete(:job_options) || {}
|
36
|
+
args.pop if args.last.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
new_job = Job.create(
|
40
|
+
:object => object.to_s,
|
41
|
+
:method_name => method_name.to_s,
|
42
|
+
:arguments => args.to_yaml,
|
43
|
+
:priority => options[:priority] || 0,
|
44
|
+
:parent_id => options[:parent_id],
|
45
|
+
:run_at => options[:run_at] || Time.now,
|
46
|
+
:created_at => Time.now
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Find the next available jobs in the queue. This will not include failed
|
52
|
+
# jobs (where :failed_at is not nil) and jobs that should run in the future
|
53
|
+
# (where :run_at is greater than the current time).
|
54
|
+
#
|
55
|
+
# @param [Integer] limit the limit of jobs to be fetched. Defaults to
|
56
|
+
# Navvy::Job.limit
|
57
|
+
#
|
58
|
+
# @return [array, nil] the next available jobs in an array or nil if no
|
59
|
+
# jobs were found.
|
60
|
+
|
61
|
+
def self.next(limit = self.limit)
|
62
|
+
all(
|
63
|
+
:failed_at => nil,
|
64
|
+
:completed_at => nil,
|
65
|
+
:run_at.lte => Time.now,
|
66
|
+
:order => [ :priority.desc, :created_at.asc ],
|
67
|
+
:limit => limit
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
|
73
|
+
# false it'll delete every completed job, if it's a timestamp it'll only
|
74
|
+
# delete completed jobs that have passed their keeptime.
|
75
|
+
#
|
76
|
+
# @return [true, false] delete_all the result of the delete_all call
|
77
|
+
|
78
|
+
def self.cleanup
|
79
|
+
if keep.is_a? Fixnum
|
80
|
+
all(:completed_at.lte => (Time.now - keep)).destroy
|
81
|
+
else
|
82
|
+
all(:completed_at.not => nil ).destroy unless keep?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Deletes all jobs.
|
88
|
+
#
|
89
|
+
# @return [true, false] deleted?
|
90
|
+
|
91
|
+
def self.delete_all
|
92
|
+
Navvy::Job.destroy
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Mark the job as started. Will set started_at to the current time.
|
97
|
+
#
|
98
|
+
# @return [true, false] update_attributes the result of the
|
99
|
+
# update_attributes call
|
100
|
+
|
101
|
+
def started
|
102
|
+
update(:started_at => Time.now)
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Mark the job as completed. Will set completed_at to the current time and
|
107
|
+
# optionally add the return value if provided.
|
108
|
+
#
|
109
|
+
# @param [String] return_value the return value you want to store.
|
110
|
+
#
|
111
|
+
# @return [true, false] update_attributes the result of the
|
112
|
+
# update_attributes call
|
113
|
+
|
114
|
+
def completed(return_value = nil)
|
115
|
+
update(
|
116
|
+
:completed_at => Time.now,
|
117
|
+
:return => return_value
|
118
|
+
)
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Mark the job as failed. Will set failed_at to the current time and
|
123
|
+
# optionally add the exception message if provided.
|
124
|
+
# optionally add the exception message if provided. Also, it will retry
|
125
|
+
# the job unless max_attempts has been reached.
|
126
|
+
#
|
127
|
+
# @param [String] exception the exception message you want to store.
|
128
|
+
#
|
129
|
+
# @return [true, false] update_attributes the result of the
|
130
|
+
# update_attributes call
|
131
|
+
|
132
|
+
def failed(message = nil)
|
133
|
+
self.retry unless times_failed >= self.class.max_attempts
|
134
|
+
update(
|
135
|
+
:failed_at => Time.now,
|
136
|
+
:exception => message
|
137
|
+
)
|
138
|
+
end
|
139
|
+
|
140
|
+
##
|
141
|
+
# Check how many times the job has failed. Will try to find jobs with a
|
142
|
+
# parent_id that's the same as self.id and count them
|
143
|
+
#
|
144
|
+
# @return [Integer] count the amount of times the job has failed
|
145
|
+
|
146
|
+
def times_failed
|
147
|
+
i = parent_id || id
|
148
|
+
klass = self.class
|
149
|
+
(klass.all(:failed_at.not => nil) & (klass.all(:id => i) | klass.all(:parent_id => i))).count
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
require 'navvy/job'
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
module Navvy
|
4
|
+
class Job
|
5
|
+
include MongoMapper::Document
|
6
|
+
|
7
|
+
key :object, String
|
8
|
+
key :method_name, Symbol
|
9
|
+
key :arguments, String
|
10
|
+
key :priority, Integer, :default => 0
|
11
|
+
key :return, String
|
12
|
+
key :exception, String
|
13
|
+
key :parent_id, ObjectId
|
14
|
+
key :created_at, Time
|
15
|
+
key :run_at, Time
|
16
|
+
key :started_at, Time
|
17
|
+
key :completed_at, Time
|
18
|
+
key :failed_at, Time
|
19
|
+
|
20
|
+
##
|
21
|
+
# Add a job to the job queue.
|
22
|
+
#
|
23
|
+
# @param [Object] object the object you want to run a method from
|
24
|
+
# @param [Symbol, String] method_name the name of the method you want to
|
25
|
+
# run
|
26
|
+
# @param [*] arguments optional arguments you want to pass to the method
|
27
|
+
#
|
28
|
+
# @return [Job, false] created Job or false if failed
|
29
|
+
|
30
|
+
def self.enqueue(object, method_name, *args)
|
31
|
+
options = {}
|
32
|
+
if args.last.is_a?(Hash)
|
33
|
+
options = args.last.delete(:job_options) || {}
|
34
|
+
args.pop if args.last.empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
create(
|
38
|
+
:object => object.to_s,
|
39
|
+
:method_name => method_name.to_sym,
|
40
|
+
:arguments => args.to_yaml,
|
41
|
+
:priority => options[:priority] || 0,
|
42
|
+
:parent_id => options[:parent_id],
|
43
|
+
:run_at => options[:run_at] || Time.now,
|
44
|
+
:created_at => Time.now
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Find the next available jobs in the queue. This will not include failed
|
50
|
+
# jobs (where :failed_at is not nil) and jobs that should run in the future
|
51
|
+
# (where :run_at is greater than the current time).
|
52
|
+
#
|
53
|
+
# @param [Integer] limit the limit of jobs to be fetched. Defaults to
|
54
|
+
# Navvy::Job.limit
|
55
|
+
#
|
56
|
+
# @return [array, nil] the next available jobs in an array or nil if no
|
57
|
+
# jobs were found.
|
58
|
+
|
59
|
+
def self.next(limit = self.limit)
|
60
|
+
all(
|
61
|
+
:failed_at => nil,
|
62
|
+
:completed_at => nil,
|
63
|
+
:run_at => {'$lte' => Time.now},
|
64
|
+
:order => 'priority desc, created_at asc',
|
65
|
+
:limit => limit
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
|
71
|
+
# false it'll delete every completed job, if it's a timestamp it'll only
|
72
|
+
# delete completed jobs that have passed their keeptime.
|
73
|
+
#
|
74
|
+
# @return [true, false] delete_all the result of the delete_all call
|
75
|
+
|
76
|
+
def self.cleanup
|
77
|
+
if keep.is_a? Fixnum
|
78
|
+
delete_all(
|
79
|
+
:completed_at => {'$lte' => keep.ago}
|
80
|
+
)
|
81
|
+
else
|
82
|
+
delete_all(
|
83
|
+
:completed_at => {'$ne' => nil}
|
84
|
+
) unless keep?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Mark the job as started. Will set started_at to the current time.
|
90
|
+
#
|
91
|
+
# @return [true, false] update_attributes the result of the
|
92
|
+
# update_attributes call
|
93
|
+
|
94
|
+
def started
|
95
|
+
update_attributes({
|
96
|
+
:started_at => Time.now
|
97
|
+
})
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Mark the job as completed. Will set completed_at to the current time and
|
102
|
+
# optionally add the return value if provided.
|
103
|
+
#
|
104
|
+
# @param [String] return_value the return value you want to store.
|
105
|
+
#
|
106
|
+
# @return [true, false] update_attributes the result of the
|
107
|
+
# update_attributes call
|
108
|
+
|
109
|
+
def completed(return_value = nil)
|
110
|
+
update_attributes({
|
111
|
+
:completed_at => Time.now,
|
112
|
+
:return => return_value
|
113
|
+
})
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
# Mark the job as failed. Will set failed_at to the current time and
|
118
|
+
# optionally add the exception message if provided. Also, it will retry
|
119
|
+
# the job unless max_attempts has been reached.
|
120
|
+
#
|
121
|
+
# @param [String] exception the exception message you want to store.
|
122
|
+
#
|
123
|
+
# @return [true, false] update_attributes the result of the
|
124
|
+
# update_attributes call
|
125
|
+
|
126
|
+
def failed(message = nil)
|
127
|
+
self.retry unless times_failed >= self.class.max_attempts
|
128
|
+
update_attributes(
|
129
|
+
:failed_at => Time.now,
|
130
|
+
:exception => message
|
131
|
+
)
|
132
|
+
end
|
133
|
+
|
134
|
+
##
|
135
|
+
# Check how many times the job has failed. Will try to find jobs with a
|
136
|
+
# parent_id that's the same as self.id and count them
|
137
|
+
#
|
138
|
+
# @return [Integer] count the amount of times the job has failed
|
139
|
+
|
140
|
+
def times_failed
|
141
|
+
i = parent_id || id
|
142
|
+
self.class.count(
|
143
|
+
:failed_at => {'$ne' => nil},
|
144
|
+
'$where' => "this._id == '#{i}' || this.parent_id == '#{i}'"
|
145
|
+
)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
require 'navvy/job'
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
module Navvy
|
4
|
+
class Job
|
5
|
+
include Mongoid::Document
|
6
|
+
include Mongoid::Timestamps
|
7
|
+
|
8
|
+
field :object, :type => String
|
9
|
+
field :method_name, :type => String
|
10
|
+
field :arguments, :type => String
|
11
|
+
field :priority, :type => Integer, :default => 0
|
12
|
+
field :return, :type => String
|
13
|
+
field :exception, :type => String
|
14
|
+
field :parent_id, :type => BSON::ObjectId
|
15
|
+
field :created_at, :type => Time
|
16
|
+
field :run_at, :type => Time
|
17
|
+
field :started_at, :type => Time
|
18
|
+
field :completed_at, :type => Time
|
19
|
+
field :failed_at, :type => Time
|
20
|
+
|
21
|
+
index [[:priority, Mongo::DESCENDING]]
|
22
|
+
index [[:created_at, Mongo::ASCENDING]]
|
23
|
+
|
24
|
+
##
|
25
|
+
# Add a job to the job queue.
|
26
|
+
#
|
27
|
+
# @param [Object] object the object you want to run a method from
|
28
|
+
# @param [Symbol, String] method_name the name of the method you want to
|
29
|
+
# run
|
30
|
+
# @param [*] arguments optional arguments you want to pass to the method
|
31
|
+
#
|
32
|
+
# @return [Job, false] created Job or false if failed
|
33
|
+
|
34
|
+
def self.enqueue(object, method_name, *args)
|
35
|
+
options = {}
|
36
|
+
if args.last.is_a?(Hash)
|
37
|
+
options = args.last.delete(:job_options) || {}
|
38
|
+
args.pop if args.last.empty?
|
39
|
+
end
|
40
|
+
|
41
|
+
create(
|
42
|
+
:object => object.to_s,
|
43
|
+
:method_name => method_name.to_sym,
|
44
|
+
:arguments => args.to_yaml,
|
45
|
+
:priority => options[:priority] || 0,
|
46
|
+
:parent_id => options[:parent_id],
|
47
|
+
:run_at => options[:run_at] || Time.now
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Find the next available jobs in the queue. This will not include failed
|
53
|
+
# jobs (where :failed_at is not nil) and jobs that should run in the future
|
54
|
+
# (where :run_at is greater than the current time).
|
55
|
+
#
|
56
|
+
# @param [Integer] limit the limit of jobs to be fetched. Defaults to
|
57
|
+
# Navvy::Job.limit
|
58
|
+
#
|
59
|
+
# @return [array, nil] the next available jobs in an array or nil if no
|
60
|
+
# jobs were found.
|
61
|
+
|
62
|
+
def self.next(limit = self.limit)
|
63
|
+
where(:failed_at => nil).
|
64
|
+
where(:completed_at => nil).
|
65
|
+
where(:run_at.lte => Time.now).
|
66
|
+
order_by([[:priority, :desc], [:created_at, :asc]]).
|
67
|
+
limit(limit).to_a
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
|
72
|
+
# false it'll delete every completed job, if it's a timestamp it'll only
|
73
|
+
# delete completed jobs that have passed their keeptime.
|
74
|
+
#
|
75
|
+
# @return [true, false] delete_all the result of the delete_all call
|
76
|
+
|
77
|
+
def self.cleanup
|
78
|
+
if keep.is_a? Fixnum
|
79
|
+
destroy_all(:conditions => { :completed_at.lte => keep.ago })
|
80
|
+
else
|
81
|
+
destroy_all(:conditions => { :completed_at.ne => nil }) unless keep?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Mark the job as started. Will set started_at to the current time.
|
87
|
+
#
|
88
|
+
# @return [true, false] update_attributes the result of the
|
89
|
+
# update_attributes call
|
90
|
+
|
91
|
+
def started
|
92
|
+
update_attributes(:started_at => Time.now)
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Mark the job as completed. Will set completed_at to the current time and
|
97
|
+
# optionally add the return value if provided.
|
98
|
+
#
|
99
|
+
# @param [String] return_value the return value you want to store.
|
100
|
+
#
|
101
|
+
# @return [true, false] update_attributes the result of the
|
102
|
+
# update_attributes call
|
103
|
+
|
104
|
+
def completed(return_value = nil)
|
105
|
+
update_attributes(:completed_at => Time.now, :return => return_value)
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Mark the job as failed. Will set failed_at to the current time and
|
110
|
+
# optionally add the exception message if provided. Also, it will retry
|
111
|
+
# the job unless max_attempts has been reached.
|
112
|
+
#
|
113
|
+
# @param [String] exception the exception message you want to store.
|
114
|
+
#
|
115
|
+
# @return [true, false] update_attributes the result of the
|
116
|
+
# update_attributes call
|
117
|
+
|
118
|
+
def failed(message = nil)
|
119
|
+
self.retry unless times_failed >= self.class.max_attempts
|
120
|
+
update_attributes(:failed_at => Time.now, :exception => message)
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# Check how many times the job has failed. Will try to find jobs with a
|
125
|
+
# parent_id that's the same as self.id and count them
|
126
|
+
#
|
127
|
+
# @return [Integer] count the amount of times the job has failed
|
128
|
+
|
129
|
+
def times_failed
|
130
|
+
i = parent_id || id
|
131
|
+
self.class.
|
132
|
+
where(:failed_at.ne => nil).
|
133
|
+
where("this._id == '#{i}' || this.parent_id == '#{i}'").
|
134
|
+
count
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
require 'navvy/job'
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Navvy
|
5
|
+
class Job < Sequel::Model
|
6
|
+
|
7
|
+
##
|
8
|
+
# Add a job to the job queue.
|
9
|
+
#
|
10
|
+
# @param [Object] object the object you want to run a method from
|
11
|
+
# @param [Symbol, String] method_name the name of the method you want to
|
12
|
+
# run
|
13
|
+
# @param [*] arguments optional arguments you want to pass to the method
|
14
|
+
#
|
15
|
+
# @return [Job, false] created Job or false if failed
|
16
|
+
|
17
|
+
def self.enqueue(object, method_name, *args)
|
18
|
+
options = {}
|
19
|
+
if args.last.is_a?(Hash)
|
20
|
+
options = args.last.delete(:job_options) || {}
|
21
|
+
args.pop if args.last.empty?
|
22
|
+
end
|
23
|
+
|
24
|
+
Job[insert(
|
25
|
+
:object => object.to_s,
|
26
|
+
:method_name => method_name.to_s,
|
27
|
+
:arguments => args.to_yaml,
|
28
|
+
:priority => options[:priority] || 0,
|
29
|
+
:parent_id => options[:parent_id],
|
30
|
+
:run_at => options[:run_at] || Time.now,
|
31
|
+
:created_at => Time.now
|
32
|
+
)]
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Find the next available jobs in the queue. This will not include failed
|
37
|
+
# jobs (where :failed_at is not nil) and jobs that should run in the future
|
38
|
+
# (where :run_at is greater than the current time).
|
39
|
+
#
|
40
|
+
# @param [Integer] limit the limit of jobs to be fetched. Defaults to
|
41
|
+
# Navvy::Job.limit
|
42
|
+
#
|
43
|
+
# @return [array, nil] the next available jobs in an array or nil if no
|
44
|
+
# jobs were found.
|
45
|
+
|
46
|
+
def self.next(limit = self.limit)
|
47
|
+
filter(:failed_at => nil).
|
48
|
+
filter(:completed_at => nil).
|
49
|
+
filter{run_at <= Time.now}.
|
50
|
+
order(:priority.desc, :created_at).
|
51
|
+
first(limit)
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
|
56
|
+
# false it'll delete every completed job, if it's a timestamp it'll only
|
57
|
+
# delete completed jobs that have passed their keeptime.
|
58
|
+
#
|
59
|
+
# @return [true, false] delete_all the result of the delete_all call
|
60
|
+
|
61
|
+
def self.cleanup
|
62
|
+
if keep.is_a? Fixnum
|
63
|
+
time = Time.now - keep
|
64
|
+
filter{completed_at <= time}.delete
|
65
|
+
else
|
66
|
+
filter(~{:completed_at => nil}).delete unless keep?
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Deletes all jobs.
|
72
|
+
#
|
73
|
+
# @return [Integer] amount the amount of jobs that were deleted
|
74
|
+
|
75
|
+
def self.delete_all
|
76
|
+
Navvy::Job.destroy
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Mark the job as started. Will set started_at to the current time.
|
81
|
+
#
|
82
|
+
# @return [true, false] update_attributes the result of the
|
83
|
+
# update_attributes call
|
84
|
+
|
85
|
+
def started
|
86
|
+
update(:started_at => Time.now)
|
87
|
+
end
|
88
|
+
|
89
|
+
##
|
90
|
+
# Mark the job as completed. Will set completed_at to the current time and
|
91
|
+
# optionally add the return value if provided.
|
92
|
+
#
|
93
|
+
# @param [String] return_value the return value you want to store.
|
94
|
+
#
|
95
|
+
# @return [true, false] update_attributes the result of the
|
96
|
+
# update_attributes call
|
97
|
+
|
98
|
+
def completed(return_value = nil)
|
99
|
+
update(
|
100
|
+
:completed_at => Time.now,
|
101
|
+
:return => return_value
|
102
|
+
)
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Mark the job as failed. Will set failed_at to the current time and
|
107
|
+
# optionally add the exception message if provided. Also, it will retry
|
108
|
+
# the job unless max_attempts has been reached.
|
109
|
+
#
|
110
|
+
# @param [String] exception the exception message you want to store.
|
111
|
+
#
|
112
|
+
# @return [true, false] update_attributes the result of the
|
113
|
+
# update_attributes call
|
114
|
+
|
115
|
+
def failed(message = nil)
|
116
|
+
self.retry unless times_failed >= self.class.max_attempts
|
117
|
+
update(
|
118
|
+
:failed_at => Time.now,
|
119
|
+
:exception => message
|
120
|
+
)
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# Check how many times the job has failed. Will try to find jobs with a
|
125
|
+
# parent_id that's the same as self.id and count them
|
126
|
+
#
|
127
|
+
# @return [Integer] count the amount of times the job has failed
|
128
|
+
|
129
|
+
def times_failed
|
130
|
+
i = parent_id || id
|
131
|
+
self.class.filter({:id => i} | {:parent_id => i}).
|
132
|
+
filter(~{:failed_at => nil}).
|
133
|
+
count
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
require 'navvy/job'
|