Skip to content

Commit

Permalink
Merge pull request lantins#27 from Talkdesk/26-dirty-exit-lock
Browse files Browse the repository at this point in the history
Explicitly remove lock upon job's dirty exit using worker's on failure hook
  • Loading branch information
lantins committed Nov 11, 2015
2 parents 5cf3ea0 + e8252b2 commit 207336d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/resque/plugins/lock_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ def around_perform_lock(*args)
end
end

def on_failure_lock(exception, *args)
# In case of a DirtyExit, the ensure block of the around hook is not called
if exception.is_a?(Resque::DirtyExit)
release_lock!(*args)
end
end
end

end
end
14 changes: 14 additions & 0 deletions test/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ def test_lock_is_released_on_failure
assert_equal false, FastJob.locked?, 'lock should have been released'
end

if Process.respond_to?(:fork) && Gem::Version.new(Resque::VERSION) >= Gem::Version.new('1.20.0')
def test_lock_is_released_on_dirty_exit
Resque.enqueue(FailingKilledJob)
job = @worker.reserve
child = fork do
Resque.redis.client.reconnect
job.perform
end
Process.waitpid(child)
job.fail(Resque::DirtyExit.new)
assert_equal false, FailingKilledJob.locked?, 'lock should have been released'
end
end

def test_can_acquire_lock_with_timeout
now = Time.now.to_i
assert SlowWithTimeoutJob.acquire_lock!, 'acquire lock'
Expand Down
10 changes: 10 additions & 0 deletions test/test_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def self.perform
end
end

# Job that fails with a SIGKILL
class FailingKilledJob
extend Resque::Plugins::LockTimeout
@queue = :test

def self.perform
Process.kill("KILL", Process.pid)
end
end

# Job that enables the timeout algorithm.
class SlowWithTimeoutJob
extend Resque::Plugins::LockTimeout
Expand Down

0 comments on commit 207336d

Please sign in to comment.