resque-multi-step 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -11,6 +11,11 @@ module Resque
11
11
  class NoSuchMultiStepTask < StandardError; end
12
12
  class NotReadyForFinalization < StandardError; end
13
13
  class FinalizationAlreadyBegun < StandardError; end
14
+ class StdOutLogger
15
+ def warn(*args); puts args; end
16
+ def info(*args); puts args; end
17
+ def debug(*args); puts args; end
18
+ end
14
19
 
15
20
  class << self
16
21
  include Constantization
@@ -34,7 +39,7 @@ module Resque
34
39
  redis.sismember("active-tasks", task_id)
35
40
  end
36
41
 
37
- # Create a brand new parallel job group.
42
+ # Create a brand new multi-step-task.
38
43
  #
39
44
  # @param [#to_s] slug The descriptive slug of the new job. Default: a
40
45
  # random UUID
@@ -52,16 +57,16 @@ module Resque
52
57
  end
53
58
  task_id << "~" << nonce
54
59
 
55
- pjg = new(task_id)
56
- pjg.nuke
60
+ mst = new(task_id)
61
+ mst.nuke
57
62
  redis.sadd("active-tasks", task_id)
58
63
  redis.sismember("active-tasks", task_id)
59
64
  if block_given?
60
65
  yield pjg
61
- pjg.finalizable!
66
+ mst.finalizable!
62
67
  end
63
68
 
64
- pjg
69
+ mst
65
70
  end
66
71
 
67
72
  # Prevent calling MultiStepTask.new
@@ -71,13 +76,13 @@ module Resque
71
76
  #
72
77
  # @param [#to_s] task_id The unique key for the job group of interest.
73
78
  #
74
- # @return [ParallelJobGroup] The group of interest
79
+ # @return [MultiStepTask] The group of interest
75
80
  #
76
81
  # @raise [NoSuchMultiStepTask] If there is not a group with the specified key.
77
82
  def find(task_id)
78
83
  raise NoSuchMultiStepTask unless active?(task_id)
79
84
 
80
- pjg = new(task_id)
85
+ mst = new(task_id)
81
86
  end
82
87
 
83
88
  # Handle job invocation
@@ -89,8 +94,16 @@ module Resque
89
94
  def perform_without_maybe_finalize(task_id, job_module_name, *args)
90
95
  task = MultiStepTask.find(task_id)
91
96
  begin
97
+ job_start_key = "#{task_id}_#{job_module_name}_#{args}-start-time-#{nonce}"
98
+ task.redis.set(job_start_key, Time.now.to_i)
99
+ logger.debug("[Resque Multi-Step-Task] Executing #{job_module_name} job for #{task_id} at #{Time.now} (args: #{args})")
100
+
101
+ # perform the task
92
102
  constantize(job_module_name).perform(*args)
103
+
104
+ logger.debug("[Resque Multi-Step-Task] Finished executing #{job_module_name} job for #{task_id} at #{Time.now}, taking #{(Time.now - task.redis.get(job_start_key).to_i).to_i} seconds.")
93
105
  rescue Exception => e
106
+ logger.error("[Resque Multi-Step-Task] #{job_module_name} job failed for #{task_id} at #{Time.now} (args: #{args})")
94
107
  task.increment_failed_count
95
108
  raise
96
109
  end
@@ -102,6 +115,14 @@ module Resque
102
115
  perform_without_maybe_finalize(task_id, job_module_name, *args)
103
116
  end
104
117
 
118
+ def logger=(logger)
119
+ @@logger = logger
120
+ end
121
+
122
+ def logger
123
+ @@logger ||= RAILS_DEFAULT_LOGGER || StdOutLogger.new
124
+ end
125
+
105
126
  # Normally jobs that are part of a multi-step task are run
106
127
  # asynchronously by putting them on a queue. However, it is
107
128
  # often more convenient to just run the jobs synchronously as
@@ -128,6 +149,7 @@ module Resque
128
149
  include Constantization
129
150
 
130
151
  attr_reader :task_id
152
+ attr_accessor :logger
131
153
 
132
154
  extend AtomicCounters
133
155
 
@@ -143,6 +165,11 @@ module Resque
143
165
  # @param [String] task_id The UUID of the group of interest.
144
166
  def initialize(task_id)
145
167
  @task_id = task_id
168
+ redis.set 'start-time', Time.now.to_i
169
+ end
170
+
171
+ def logger
172
+ self.class.logger
146
173
  end
147
174
 
148
175
  def redis
@@ -171,6 +198,7 @@ module Resque
171
198
  # @param [Class,Module] job_type The type of the job to be performed.
172
199
  def add_job(job_type, *args)
173
200
  increment_normal_job_count
201
+ logger.debug("[Resque Multi-Step-Task] Adding #{job_type} job for #{task_id} (args: #{args})")
174
202
 
175
203
  if synchronous?
176
204
  self.class.perform(task_id, job_type.to_s, *args)
@@ -186,6 +214,7 @@ module Resque
186
214
  # @param [Class,Module] job_type The type of job to be performed.
187
215
  def add_finalization_job(job_type, *args)
188
216
  increment_finalize_job_count
217
+ logger.debug("[Resque Multi-Step-Task] Adding #{job_type} finalization job for #{task_id} (args: #{args})")
189
218
 
190
219
  redis.rpush 'finalize_jobs', Yajl::Encoder.encode([job_type.to_s, *args])
191
220
  end
@@ -217,6 +246,7 @@ module Resque
217
246
  # @raise [FinalizationAlreadyBegun] If some other process has
218
247
  # already started (and/or finished) the finalization process.
219
248
  def finalize!
249
+ logger.debug("[Resque Multi-Step-Task] Attempting to finalize #{task_id}")
220
250
  raise FinalizationAlreadyBegun unless MultiStepTask.active?(task_id)
221
251
  raise NotReadyForFinalization if !ready_for_finalization? || incomplete_because_of_errors?
222
252
 
@@ -233,6 +263,7 @@ module Resque
233
263
  Resque::Job.create(queue_name, FinalizationJob, self.task_id, *fin_job_info)
234
264
  else
235
265
  # There is nothing left to do so cleanup.
266
+ logger.debug "[Resque Multi-Step-Task] \"#{task_id}\" finalized successfully at #{Time.now}, taking #{(Time.now - redis.get('start-time').to_i).to_i} seconds."
236
267
  nuke
237
268
  end
238
269
  end
@@ -243,6 +274,8 @@ module Resque
243
274
  job_class_name, *args = Yajl::Parser.parse(fin_job_info)
244
275
  self.class.perform_finalization(task_id, job_class_name, *args)
245
276
  end
277
+
278
+ logger.debug "[Resque Multi-Step-Task] \"#{task_id}\" finalized successfully at #{Time.now}, taking #{(Time.now - redis.get('start-time').to_i).to_i} seconds."
246
279
  nuke
247
280
  end
248
281
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{resque-multi-step}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Williams", "Morgan Whitney"]
12
- s.date = %q{2011-01-31}
12
+ s.date = %q{2011-02-18}
13
13
  s.description = %q{Provides multi-step tasks with finalization and progress tracking}
14
14
  s.email = %q{pezra@barelyenough.org}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-multi-step
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Williams
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-31 00:00:00 -07:00
19
+ date: 2011-02-18 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency