forked from resque/resque-loner
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jannis Hermanns
committed
Jun 15, 2010
1 parent
81cedcc
commit 654bdbf
Showing
6 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
require 'rubygems' | ||
require 'resque' | ||
require 'lib/resque_loner/loner_job' | ||
require 'lib/resque_loner/resque' | ||
|
||
Resque.class_eval do | ||
include ResqueLoner::Resque | ||
end | ||
require 'lib/resque_loner/helpers' | ||
require 'lib/resque_loner/job' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Resque | ||
module Plugins | ||
module Loner | ||
class Helpers | ||
extend Resque::Helpers | ||
|
||
def self.loner_queued?(queue, item) | ||
return false unless item_is_a_loner_job?(item) | ||
redis.get(loner_job_queue_key(queue, item)) == "1" | ||
end | ||
|
||
def self.mark_loner_as_queued(queue, item) | ||
return unless item_is_a_loner_job?(item) | ||
redis.set(loner_job_queue_key(queue, item), 1) | ||
end | ||
|
||
def self.mark_loner_as_unqueued(queue, job) | ||
return unless item_is_a_loner_job?(job.payload) | ||
redis.del(loner_job_queue_key(queue, job.payload)) | ||
end | ||
|
||
def self.loner_job_queue_key(queue, item) | ||
job_key = constantize(item[:class] || item["class"]).redis_key(item) | ||
"loners:queue:#{queue}:job:#{job_key}" | ||
end | ||
|
||
def self.item_is_a_loner_job?(item) | ||
klass = constantize(item[:class] || item["class"]) | ||
klass.ancestors.include?(::Resque::Plugins::Loner::LonerJob) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Resque | ||
# module Plugins | ||
# module Loner | ||
class Job | ||
|
||
|
||
# | ||
# Overwriting original create method to mark an item as queued | ||
# after Resque::Job.create has called Resque.push | ||
# | ||
def self.create_with_loner(queue, klass, *args) | ||
item = { :class => klass.to_s, :args => args } | ||
return "EXISTED" if Resque::Plugins::Loner::Helpers.loner_queued?(queue, item) | ||
create_without_loner(queue, klass, *args) | ||
Resque::Plugins::Loner::Helpers.mark_loner_as_queued(queue, item) | ||
end | ||
|
||
# | ||
# Overwriting original reserve method to mark an item as unqueued | ||
# | ||
def self.reserve_with_loner(queue) | ||
item = reserve_without_loner(queue) | ||
Resque::Plugins::Loner::Helpers.mark_loner_as_unqueued( queue, item ) if item | ||
item | ||
end | ||
|
||
# | ||
# Overwriting original destroy method to mark all destroyed jobs as unqueued | ||
# | ||
def self.destroy_with_loner(queue, klass, *args) | ||
destroyed = destroy_without_loner(queue, klass, *args) | ||
|
||
destroyed | ||
end | ||
|
||
class << self | ||
alias_method :create_without_loner, :create | ||
alias_method :create, :create_with_loner | ||
alias_method :reserve_without_loner, :reserve | ||
alias_method :reserve, :reserve_with_loner | ||
alias_method :destroy_without_loner, :destroy | ||
alias_method :destroy, :destroy_with_loner | ||
end | ||
end | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
require 'digest/md5' | ||
|
||
module Resque | ||
class LonerJob | ||
extend Resque::Helpers | ||
|
||
def self.redis_key(values) | ||
Digest::MD5.hexdigest encode(values) | ||
module Plugins | ||
module Loner | ||
class LonerJob | ||
extend Resque::Helpers | ||
|
||
def self.redis_key(item) | ||
job = item[:class] || item["class"] | ||
args = item[:args] || item["args"] | ||
digest = Digest::MD5.hexdigest encode(:class => job, :args => args) | ||
digest | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters