navvy 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,11 +12,9 @@ begin
12
12
  gem.authors = ["Jeff Kreeftmeijer"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
14
  gem.add_development_dependency "yard", ">= 0.5.2"
15
- gem.add_development_dependency "metric_fu", ">= 1.1.6"
16
- gem.add_development_dependency "machinist", ">= 1.0.6"
17
15
  gem.add_development_dependency "mongo_mapper", ">= 0.6.10"
18
- gem.add_development_dependency "machinist_mongomapper", ">= 0.9.7"
19
16
  gem.add_development_dependency "sequel", ">= 3.8.0"
17
+ gem.add_development_dependency "sqlite3-ruby", ">= 1.2.5"
20
18
  end
21
19
  Jeweler::GemcutterTasks.new
22
20
  rescue LoadError
@@ -26,22 +24,26 @@ end
26
24
  require 'spec/rake/spectask'
27
25
 
28
26
  task :spec do
29
- ['spec:active_record', 'spec:mongo_mapper', 'spec:sequel'].each do |spec|
27
+ ['spec:active_record', 'spec:mongo_mapper', 'spec:sequel', 'spec:data_mapper'].each do |spec|
30
28
  Rake::Task[spec].invoke
31
29
  end
32
30
  end
33
31
 
34
32
  namespace :spec do
35
33
  Spec::Rake::SpecTask.new(:active_record) do |spec|
36
- spec.spec_files = FileList['spec/job/active_record_spec.rb', 'spec/*_spec.rb']
34
+ spec.spec_files = FileList['spec/setup/active_record.rb', 'spec/*_spec.rb']
37
35
  end
38
36
 
39
37
  Spec::Rake::SpecTask.new(:mongo_mapper) do |spec|
40
- spec.spec_files = FileList['spec/job/mongo_mapper_spec.rb', 'spec/*_spec.rb']
38
+ spec.spec_files = FileList['spec/setup/mongo_mapper.rb', 'spec/*_spec.rb']
41
39
  end
42
40
 
43
41
  Spec::Rake::SpecTask.new(:sequel) do |spec|
44
- spec.spec_files = FileList['spec/job/sequel_spec.rb', 'spec/*_spec.rb']
42
+ spec.spec_files = FileList['spec/setup/sequel.rb', 'spec/*_spec.rb']
43
+ end
44
+
45
+ Spec::Rake::SpecTask.new(:data_mapper) do |spec|
46
+ spec.spec_files = FileList['spec/setup/data_mapper.rb', 'spec/*_spec.rb']
45
47
  end
46
48
  end
47
49
 
@@ -62,5 +64,3 @@ rescue LoadError
62
64
  abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
63
65
  end
64
66
  end
65
-
66
- require 'metric_fu'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,6 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'active_record'
3
- require 'yaml'
4
3
 
5
4
  module Navvy
6
5
  class Job < ActiveRecord::Base
@@ -89,7 +88,7 @@ module Navvy
89
88
  ) unless keep?
90
89
  end
91
90
  end
92
-
91
+
93
92
  ##
94
93
  # Run the job. Will delete the Navvy::Job record and return its return
95
94
  # value if it runs successfully unless Navvy::Job.keep is set. If a job
@@ -110,69 +109,69 @@ module Navvy
110
109
  result = object.constantize.send(method_name)
111
110
  else
112
111
  result = object.constantize.send(method_name, *args)
113
- end
112
+ end
114
113
  Navvy::Job.keep? ? completed : destroy
115
114
  result
116
115
  rescue Exception => exception
117
116
  failed(exception.message)
118
117
  end
119
118
  end
120
-
119
+
121
120
  ##
122
- # Mark the job as completed. Will set completed_at to the current time and
121
+ # Mark the job as completed. Will set completed_at to the current time and
123
122
  # optionally add the return value if provided.
124
123
  #
125
124
  # @param [String] return_value the return value you want to store.
126
125
  #
127
126
  # @return [true, false] update_attributes the result of the
128
127
  # update_attributes call
129
-
128
+
130
129
  def completed(return_value = nil)
131
130
  update_attributes({
132
131
  :completed_at => Time.now,
133
132
  :return => return_value
134
133
  })
135
134
  end
136
-
135
+
137
136
  ##
138
- # Mark the job as failed. Will set failed_at to the current time and
137
+ # Mark the job as failed. Will set failed_at to the current time and
139
138
  # optionally add the exception message if provided.
140
139
  #
141
140
  # @param [String] exception the exception message you want to store.
142
141
  #
143
142
  # @return [true, false] update_attributes the result of the
144
143
  # update_attributes call
145
-
144
+
146
145
  def failed(message = nil)
147
146
  update_attributes({
148
147
  :failed_at => Time.now,
149
148
  :exception => message
150
149
  })
151
150
  end
152
-
151
+
153
152
  ##
154
- # Check if the job has been run.
153
+ # Check if the job has been run.
155
154
  #
156
155
  # @return [true, false] ran
157
-
156
+
158
157
  def ran?
159
158
  completed? || failed?
160
159
  end
161
-
160
+
162
161
  ##
163
162
  # Check how long it took for a job to complete or fail
164
163
  #
165
164
  # @return [Time, Integer] time the time it took
166
-
165
+
167
166
  def duration
168
167
  ran? ? (completed_at || failed_at) - started_at : 0
169
168
  end
170
-
169
+
171
170
  ##
172
171
  # Check if completed_at is set
173
172
  #
174
173
  # @return [true, false] set?
175
-
174
+
176
175
  def completed_at?
177
176
  !completed_at.nil?
178
177
  end
@@ -181,31 +180,20 @@ module Navvy
181
180
  # Check if failed_at is set
182
181
  #
183
182
  # @return [true, false] set?
184
-
183
+
185
184
  def failed_at?
186
185
  !failed_at.nil?
187
186
  end
188
-
187
+
189
188
  ##
190
189
  # Get the job arguments as an array
191
190
  #
192
191
  # @return [array] arguments
193
-
192
+
194
193
  def args
195
194
  arguments.is_a?(Array) ? arguments : YAML.load(arguments)
196
195
  end
197
-
198
- ##
199
- # Get the job status
200
- #
201
- # @return [:pending, :completed, :failed] status
202
-
203
- def status
204
- return :completed if completed?
205
- return :failed if failed?
206
- :pending
207
- end
208
-
196
+
209
197
  alias_method :completed?, :completed_at?
210
198
  alias_method :failed?, :failed_at?
211
199
  end
@@ -0,0 +1,213 @@
1
+ require 'rubygems'
2
+ require 'dm-core'
3
+
4
+ module Navvy
5
+ class Job
6
+ include DataMapper::Resource
7
+
8
+ class << self
9
+ attr_writer :limit
10
+ attr_accessor :keep
11
+ end
12
+
13
+ property :id, Serial
14
+ property :object, String
15
+ property :method_name, String
16
+ property :arguments, String
17
+ property :priority, Integer, :default => 0
18
+ property :return, String
19
+ property :exception, String
20
+ property :created_at, Time
21
+ property :run_at, Time
22
+ property :started_at, Time
23
+ property :completed_at, Time
24
+ property :failed_at, Time
25
+
26
+ ##
27
+ # Default limit of jobs to be fetched
28
+ #
29
+ # @return [Integer] limit
30
+
31
+ def self.limit
32
+ @limit || 100
33
+ end
34
+
35
+ ##
36
+ # Should the job be kept?
37
+ #
38
+ # @return [true, false] keep
39
+
40
+ def self.keep?
41
+ keep = (@keep || false)
42
+ return (Time.now + keep) >= Time.now if keep.is_a? Fixnum
43
+ keep
44
+ end
45
+
46
+ ##
47
+ # Add a job to the job queue.
48
+ #
49
+ # @param [Object] object the object you want to run a method from
50
+ # @param [Symbol, String] method_name the name of the method you want to
51
+ # run
52
+ # @param [*] arguments optional arguments you want to pass to the method
53
+ #
54
+ # @return [true, false]
55
+
56
+ def self.enqueue(object, method_name, *args)
57
+ new_job = self.new
58
+ new_job.attributes = {
59
+ :object => object.to_s,
60
+ :method_name => method_name.to_s,
61
+ :arguments => args.to_yaml,
62
+ :run_at => Time.now,
63
+ :created_at => Time.now
64
+ }
65
+ new_job.save
66
+ new_job
67
+ end
68
+
69
+ ##
70
+ # Find the next available jobs in the queue. This will not include failed
71
+ # jobs (where :failed_at is not nil) and jobs that should run in the future
72
+ # (where :run_at is greater than the current time).
73
+ #
74
+ # @param [Integer] limit the limit of jobs to be fetched. Defaults to
75
+ # Navvy::Job.limit
76
+ #
77
+ # @return [array, nil] the next available jobs in an array or nil if no
78
+ # jobs were found.
79
+
80
+ def self.next(limit = self.limit)
81
+ all(
82
+ :failed_at => nil,
83
+ :completed_at => nil,
84
+ :run_at.lte => Time.now,
85
+ :order => [ :priority.desc, :created_at.asc ],
86
+ :limit => limit
87
+ )
88
+ end
89
+
90
+ ##
91
+ # Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
92
+ # false it'll delete every completed job, if it's a timestamp it'll only
93
+ # delete completed jobs that have passed their keeptime.
94
+ #
95
+ # @return [true, false] delete_all the result of the delete_all call
96
+
97
+ def self.cleanup
98
+ if keep.is_a? Fixnum
99
+ all( :completed_at.lte => (Time.now - keep)).destroy
100
+ else
101
+ all( :completed_at.not => nil ).destroy unless keep?
102
+ end
103
+ end
104
+
105
+ ##
106
+ # Run the job. Will delete the Navvy::Job record and return its return
107
+ # value if it runs successfully unless Navvy::Job.keep is set. If a job
108
+ # fails, it'll update the Navvy::Job record to include the exception
109
+ # message it sent back and set the :failed_at date. Failed jobs never get
110
+ # deleted.
111
+ #
112
+ # @example
113
+ # job = Navvy::Job.next # finds the next available job in the queue
114
+ # job.run # runs the job and returns the job's return value
115
+ #
116
+ # @return [String] return value of the called method.
117
+
118
+ def run
119
+ begin
120
+ update(:started_at => Time.now)
121
+ if args.empty?
122
+ result = Kernel.const_get(object).send(method_name)
123
+ else
124
+ result = Kernel.const_get(object).send(method_name, *args)
125
+ end
126
+ Navvy::Job.keep? ? completed : destroy
127
+ result
128
+ rescue Exception => exception
129
+ failed(exception.message)
130
+ end
131
+ end
132
+
133
+ ##
134
+ # Mark the job as completed. Will set completed_at to the current time and
135
+ # optionally add the return value if provided.
136
+ #
137
+ # @param [String] return_value the return value you want to store.
138
+ #
139
+ # @return [true, false] update_attributes the result of the
140
+ # update_attributes call
141
+
142
+ def completed(return_value = nil)
143
+ update(
144
+ :completed_at => Time.now,
145
+ :return => return_value
146
+ )
147
+ end
148
+
149
+ ##
150
+ # Mark the job as failed. Will set failed_at to the current time and
151
+ # optionally add the exception message if provided.
152
+ #
153
+ # @param [String] exception the exception message you want to store.
154
+ #
155
+ # @return [true, false] update_attributes the result of the
156
+ # update_attributes call
157
+
158
+ def failed(message = nil)
159
+ update(
160
+ :failed_at => Time.now,
161
+ :exception => message
162
+ )
163
+ end
164
+
165
+ ##
166
+ # Check if the job has been run.
167
+ #
168
+ # @return [true, false] ran
169
+
170
+ def ran?
171
+ completed? || failed?
172
+ end
173
+
174
+ ##
175
+ # Check if completed_at is set
176
+ #
177
+ # @return [true, false] set?
178
+
179
+ def completed_at?
180
+ !completed_at.nil?
181
+ end
182
+
183
+ ##
184
+ # Check if failed_at is set
185
+ #
186
+ # @return [true, false] set?
187
+
188
+ def failed_at?
189
+ !failed_at.nil?
190
+ end
191
+
192
+ ##
193
+ # Check how long it took for a job to complete or fail
194
+ #
195
+ # @return [Time, Integer] time the time it took
196
+
197
+ def duration
198
+ ran? ? (completed_at || failed_at) - started_at : 0
199
+ end
200
+
201
+ ##
202
+ # Get the job arguments as an array
203
+ #
204
+ # @return [array] arguments
205
+
206
+ def args
207
+ arguments.is_a?(Array) ? arguments : YAML.load(arguments)
208
+ end
209
+
210
+ alias_method :completed?, :completed_at?
211
+ alias_method :failed?, :failed_at?
212
+ end
213
+ end
@@ -19,7 +19,7 @@ module Navvy
19
19
  key :started_at, Time
20
20
  key :completed_at, Time
21
21
  key :failed_at, Time
22
-
22
+
23
23
  ##
24
24
  # Default limit of jobs to be fetched
25
25
  #
@@ -44,7 +44,7 @@ module Navvy
44
44
  # Add a job to the job queue.
45
45
  #
46
46
  # @param [Object] object the object you want to run a method from
47
- # @param [Symbol, String] method_name the name of the method you want to
47
+ # @param [Symbol, String] method_name the name of the method you want to
48
48
  # run
49
49
  # @param [*] arguments optional arguments you want to pass to the method
50
50
  #
@@ -75,7 +75,7 @@ module Navvy
75
75
  all(
76
76
  :failed_at => nil,
77
77
  :completed_at => nil,
78
- :run_at => {'$lte', Time.now},
78
+ :run_at => {'$lte' => Time.now},
79
79
  :limit => limit,
80
80
  :order => 'created_at'
81
81
  )
@@ -127,70 +127,67 @@ module Navvy
127
127
  failed(exception.message)
128
128
  end
129
129
  end
130
-
130
+
131
131
  ##
132
- # Mark the job as completed. Will set completed_at to the current time and
132
+ # Mark the job as completed. Will set completed_at to the current time and
133
133
  # optionally add the return value if provided.
134
134
  #
135
135
  # @param [String] return_value the return value you want to store.
136
136
  #
137
137
  # @return [true, false] update_attributes the result of the
138
138
  # update_attributes call
139
-
139
+
140
140
  def completed(return_value = nil)
141
141
  update_attributes({
142
142
  :completed_at => Time.now,
143
143
  :return => return_value
144
144
  })
145
145
  end
146
-
146
+
147
147
  ##
148
- # Mark the job as failed. Will set failed_at to the current time and
148
+ # Mark the job as failed. Will set failed_at to the current time and
149
149
  # optionally add the exception message if provided.
150
150
  #
151
151
  # @param [String] exception the exception message you want to store.
152
152
  #
153
153
  # @return [true, false] update_attributes the result of the
154
154
  # update_attributes call
155
-
155
+
156
156
  def failed(message = nil)
157
157
  update_attributes({
158
158
  :failed_at => Time.now,
159
159
  :exception => message
160
160
  })
161
161
  end
162
-
162
+
163
163
  ##
164
- # Check if the job has been run.
164
+ # Check if the job has been run.
165
165
  #
166
166
  # @return [true, false] ran
167
-
167
+
168
168
  def ran?
169
169
  completed? || failed?
170
170
  end
171
-
171
+
172
172
  ##
173
173
  # Check how long it took for a job to complete or fail
174
174
  #
175
175
  # @return [Time, Integer] time the time it took
176
-
176
+
177
177
  def duration
178
178
  ran? ? (completed_at || failed_at) - started_at : 0
179
179
  end
180
-
180
+
181
181
  ##
182
- # Get the job status
183
- #
184
- # @return [:pending, :completed, :failed] status
185
-
186
- def status
187
- return :completed if completed?
188
- return :failed if failed?
189
- :pending
182
+ # Get the job arguments as an array
183
+ #
184
+ # @return [array] arguments
185
+
186
+ def args
187
+ arguments.is_a?(Array) ? arguments : YAML.load(arguments)
190
188
  end
191
-
189
+
192
190
  alias_method :completed?, :completed_at?
193
191
  alias_method :failed?, :failed_at?
194
- alias_method :args, :arguments
195
192
  end
196
193
  end
@@ -33,7 +33,7 @@ module Navvy
33
33
  # Add a job to the job queue.
34
34
  #
35
35
  # @param [Object] object the object you want to run a method from
36
- # @param [Symbol, String] method_name the name of the method you want to
36
+ # @param [Symbol, String] method_name the name of the method you want to
37
37
  # run
38
38
  # @param [*] arguments optional arguments you want to pass to the method
39
39
  #
@@ -43,7 +43,7 @@ module Navvy
43
43
  create(
44
44
  :object => object.to_s,
45
45
  :method_name => method_name.to_s,
46
- :arguments => YAML::dump(args),
46
+ :arguments => args.to_yaml,
47
47
  :run_at => Time.now,
48
48
  :created_at => Time.now
49
49
  )
@@ -81,7 +81,7 @@ module Navvy
81
81
  filter('`completed_at` IS NOT NULL').delete unless keep?
82
82
  end
83
83
  end
84
-
84
+
85
85
  ##
86
86
  # Run the job. Will delete the Navvy::Job record and return its return
87
87
  # value if it runs successfully unless Navvy::Job.keep is set. If a job
@@ -105,62 +105,62 @@ module Navvy
105
105
  failed(exception.message)
106
106
  end
107
107
  end
108
-
108
+
109
109
  ##
110
- # Mark the job as completed. Will set completed_at to the current time and
110
+ # Mark the job as completed. Will set completed_at to the current time and
111
111
  # optionally add the return value if provided.
112
112
  #
113
113
  # @param [String] return_value the return value you want to store.
114
114
  #
115
115
  # @return [true, false] update_attributes the result of the
116
116
  # update_attributes call
117
-
117
+
118
118
  def completed(return_value = nil)
119
119
  update({
120
120
  :completed_at => Time.now,
121
121
  :return => return_value
122
122
  })
123
123
  end
124
-
124
+
125
125
  ##
126
- # Mark the job as failed. Will set failed_at to the current time and
126
+ # Mark the job as failed. Will set failed_at to the current time and
127
127
  # optionally add the exception message if provided.
128
128
  #
129
129
  # @param [String] exception the exception message you want to store.
130
130
  #
131
131
  # @return [true, false] update_attributes the result of the
132
132
  # update_attributes call
133
-
133
+
134
134
  def failed(message = nil)
135
135
  update({
136
136
  :failed_at => Time.now,
137
137
  :exception => message
138
138
  })
139
139
  end
140
-
140
+
141
141
  ##
142
- # Check if the job has been run.
142
+ # Check if the job has been run.
143
143
  #
144
144
  # @return [true, false] ran
145
-
145
+
146
146
  def ran?
147
147
  completed? || failed?
148
148
  end
149
-
149
+
150
150
  ##
151
151
  # Check how long it took for a job to complete or fail
152
152
  #
153
153
  # @return [Time, Integer] time the time it took
154
-
154
+
155
155
  def duration
156
156
  ran? ? (completed_at || failed_at) - started_at : 0
157
157
  end
158
-
158
+
159
159
  ##
160
160
  # Check if completed_at is set
161
161
  #
162
162
  # @return [true, false] set?
163
-
163
+
164
164
  def completed_at?
165
165
  !completed_at.nil?
166
166
  end
@@ -169,31 +169,20 @@ module Navvy
169
169
  # Check if failed_at is set
170
170
  #
171
171
  # @return [true, false] set?
172
-
172
+
173
173
  def failed_at?
174
174
  !failed_at.nil?
175
175
  end
176
-
176
+
177
177
  ##
178
178
  # Get the job arguments as an array
179
179
  #
180
180
  # @return [array] arguments
181
-
181
+
182
182
  def args
183
- arguments.first.is_a?(Array) ? arguments : YAML.load(arguments)
184
- end
185
-
186
- ##
187
- # Get the job status
188
- #
189
- # @return [:pending, :completed, :failed] status
190
-
191
- def status
192
- return :completed if completed?
193
- return :failed if failed?
194
- :pending
183
+ arguments.is_a?(Array) ? arguments : YAML.load(arguments)
195
184
  end
196
-
185
+
197
186
  alias_method :completed?, :completed_at?
198
187
  alias_method :failed?, :failed_at?
199
188
  end