Skip to content

Commit a926308

Browse files
srdefunkt
authored andcommitted
Move done_working to the parent process
When the child is killed, the redis key isn't removed. We believe the right way to fix it would be to move all state-related code to the parent process.
1 parent eb6faa3 commit a926308

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

lib/resque/worker.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ def work(interval = 5, &block)
113113
if not @paused and job = reserve
114114
log "got: #{job.inspect}"
115115
run_hook :before_fork
116+
working_on job
116117

117118
if @child = fork
118119
rand # Reseeding
119120
procline "Forked #{@child} at #{Time.now.to_i}"
120121
Process.wait
121122
else
122123
procline "Processing #{job.queue} since #{Time.now.to_i}"
123-
process(job, &block)
124+
perform(job, &block)
124125
exit! unless @cant_fork
125126
end
126127

128+
done_working
127129
@child = nil
128130
else
129131
break if interval.to_i == 0
@@ -137,15 +139,21 @@ def work(interval = 5, &block)
137139
unregister_worker
138140
end
139141

140-
# Processes a single job. If none is given, it will try to produce
141-
# one.
142-
def process(job = nil)
142+
# DEPRECATED. Processes a single job. If none is given, it will
143+
# try to produce one. Usually run in the child.
144+
def process(job = nil, &block)
143145
return unless job ||= reserve
144146

147+
working_on job
148+
perform(job, &block)
149+
ensure
150+
done_working
151+
end
152+
153+
# Processes a given job in the child.
154+
def perform(job)
145155
begin
146-
job.worker = self
147156
run_hook :after_fork, job
148-
working_on job
149157
job.perform
150158
rescue Object => e
151159
log "#{job.inspect} failed: #{e.inspect}"
@@ -155,7 +163,6 @@ def process(job = nil)
155163
log "done: #{job.inspect}"
156164
ensure
157165
yield job if block_given?
158-
done_working
159166
end
160167
end
161168

@@ -347,6 +354,7 @@ def unregister_worker
347354
# Given a job, tells Redis we're working on it. Useful for seeing
348355
# what workers are doing and when.
349356
def working_on(job)
357+
job.worker = self
350358
data = encode \
351359
:queue => job.queue,
352360
:run_at => Time.now.to_s,

test/resque_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
@worker = Resque::Worker.new(:jobs)
203203
@worker.register_worker
204204
2.times { @worker.process }
205+
205206
job = @worker.reserve
206207
@worker.working_on job
207208

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ class BadJobWithSyntaxError
108108
def self.perform
109109
raise SyntaxError, "Extra Bad job!"
110110
end
111-
end
111+
end

0 commit comments

Comments
 (0)