delayed_job_unique_key 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/delayed_job_unique_key.rb +8 -0
- data/lib/delayed_job_unique_key/active_record_job.rb +12 -0
- data/lib/delayed_job_unique_key/base.rb +52 -0
- data/lib/delayed_job_unique_key/version.rb +3 -0
- data/lib/generators/delayed_job_unique_key/install_generator.rb +17 -0
- data/lib/generators/delayed_job_unique_key/templates/add_unique_key_migration.rb +11 -0
- metadata +63 -174
- data/MIT-LICENSE +0 -20
- data/README.textile +0 -246
- data/contrib/delayed_job.monitrc +0 -14
- data/contrib/delayed_job_multiple.monitrc +0 -23
- data/lib/delayed/backend/base.rb +0 -152
- data/lib/delayed/backend/shared_spec.rb +0 -566
- data/lib/delayed/command.rb +0 -101
- data/lib/delayed/deserialization_error.rb +0 -4
- data/lib/delayed/lifecycle.rb +0 -84
- data/lib/delayed/message_sending.rb +0 -54
- data/lib/delayed/performable_mailer.rb +0 -21
- data/lib/delayed/performable_method.rb +0 -33
- data/lib/delayed/plugin.rb +0 -15
- data/lib/delayed/plugins/clear_locks.rb +0 -15
- data/lib/delayed/psych_ext.rb +0 -75
- data/lib/delayed/railtie.rb +0 -16
- data/lib/delayed/recipes.rb +0 -50
- data/lib/delayed/serialization/active_record.rb +0 -19
- data/lib/delayed/syck_ext.rb +0 -34
- data/lib/delayed/tasks.rb +0 -11
- data/lib/delayed/worker.rb +0 -222
- data/lib/delayed/yaml_ext.rb +0 -10
- data/lib/delayed_job.rb +0 -22
- data/lib/generators/delayed_job/delayed_job_generator.rb +0 -11
- data/lib/generators/delayed_job/templates/script +0 -5
- data/recipes/delayed_job.rb +0 -1
- data/spec/autoloaded/clazz.rb +0 -7
- data/spec/autoloaded/instance_clazz.rb +0 -6
- data/spec/autoloaded/instance_struct.rb +0 -6
- data/spec/autoloaded/struct.rb +0 -7
- data/spec/delayed/backend/test.rb +0 -113
- data/spec/delayed/serialization/test.rb +0 -0
- data/spec/fixtures/bad_alias.yml +0 -1
- data/spec/lifecycle_spec.rb +0 -107
- data/spec/message_sending_spec.rb +0 -116
- data/spec/performable_mailer_spec.rb +0 -46
- data/spec/performable_method_spec.rb +0 -89
- data/spec/sample_jobs.rb +0 -75
- data/spec/spec_helper.rb +0 -45
- data/spec/test_backend_spec.rb +0 -13
- data/spec/worker_spec.rb +0 -19
- data/spec/yaml_ext_spec.rb +0 -41
data/lib/delayed/command.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'daemons'
|
2
|
-
require 'optparse'
|
3
|
-
|
4
|
-
module Delayed
|
5
|
-
class Command
|
6
|
-
attr_accessor :worker_count
|
7
|
-
|
8
|
-
def initialize(args)
|
9
|
-
@options = {
|
10
|
-
:quiet => true,
|
11
|
-
:pid_dir => "#{Rails.root}/tmp/pids"
|
12
|
-
}
|
13
|
-
|
14
|
-
@worker_count = 1
|
15
|
-
@monitor = false
|
16
|
-
|
17
|
-
opts = OptionParser.new do |opts|
|
18
|
-
opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
|
19
|
-
|
20
|
-
opts.on('-h', '--help', 'Show this message') do
|
21
|
-
puts opts
|
22
|
-
exit 1
|
23
|
-
end
|
24
|
-
opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
|
25
|
-
STDERR.puts "The -e/--environment option has been deprecated and has no effect. Use RAILS_ENV and see http://github.com/collectiveidea/delayed_job/issues/#issue/7"
|
26
|
-
end
|
27
|
-
opts.on('--min-priority N', 'Minimum priority of jobs to run.') do |n|
|
28
|
-
@options[:min_priority] = n
|
29
|
-
end
|
30
|
-
opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n|
|
31
|
-
@options[:max_priority] = n
|
32
|
-
end
|
33
|
-
opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count|
|
34
|
-
@worker_count = worker_count.to_i rescue 1
|
35
|
-
end
|
36
|
-
opts.on('--pid-dir=DIR', 'Specifies an alternate directory in which to store the process ids.') do |dir|
|
37
|
-
@options[:pid_dir] = dir
|
38
|
-
end
|
39
|
-
opts.on('-i', '--identifier=n', 'A numeric identifier for the worker.') do |n|
|
40
|
-
@options[:identifier] = n
|
41
|
-
end
|
42
|
-
opts.on('-m', '--monitor', 'Start monitor process.') do
|
43
|
-
@monitor = true
|
44
|
-
end
|
45
|
-
opts.on('--sleep-delay N', "Amount of time to sleep when no jobs are found") do |n|
|
46
|
-
@options[:sleep_delay] = n
|
47
|
-
end
|
48
|
-
opts.on('-p', '--prefix NAME', "String to be prefixed to worker process names") do |prefix|
|
49
|
-
@options[:prefix] = prefix
|
50
|
-
end
|
51
|
-
opts.on('--queues=queues', "Specify which queue DJ must look up for jobs") do |queues|
|
52
|
-
@options[:queues] = queues.split(',')
|
53
|
-
end
|
54
|
-
opts.on('--queue=queue', "Specify which queue DJ must look up for jobs") do |queue|
|
55
|
-
@options[:queues] = queue.split(',')
|
56
|
-
end
|
57
|
-
end
|
58
|
-
@args = opts.parse!(args)
|
59
|
-
end
|
60
|
-
|
61
|
-
def daemonize
|
62
|
-
dir = @options[:pid_dir]
|
63
|
-
Dir.mkdir(dir) unless File.exists?(dir)
|
64
|
-
|
65
|
-
if @worker_count > 1 && @options[:identifier]
|
66
|
-
raise ArgumentError, 'Cannot specify both --number-of-workers and --identifier'
|
67
|
-
elsif @worker_count == 1 && @options[:identifier]
|
68
|
-
process_name = "delayed_job.#{@options[:identifier]}"
|
69
|
-
run_process(process_name, dir)
|
70
|
-
else
|
71
|
-
worker_count.times do |worker_index|
|
72
|
-
process_name = worker_count == 1 ? "delayed_job" : "delayed_job.#{worker_index}"
|
73
|
-
run_process(process_name, dir)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def run_process(process_name, dir)
|
79
|
-
Delayed::Worker.before_fork
|
80
|
-
Daemons.run_proc(process_name, :dir => dir, :dir_mode => :normal, :monitor => @monitor, :ARGV => @args) do |*args|
|
81
|
-
$0 = File.join(@options[:prefix], process_name) if @options[:prefix]
|
82
|
-
run process_name
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def run(worker_name = nil)
|
87
|
-
Dir.chdir(Rails.root)
|
88
|
-
|
89
|
-
Delayed::Worker.after_fork
|
90
|
-
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
|
91
|
-
|
92
|
-
worker = Delayed::Worker.new(@options)
|
93
|
-
worker.name_prefix = "#{worker_name} "
|
94
|
-
worker.start
|
95
|
-
rescue => e
|
96
|
-
Rails.logger.fatal e
|
97
|
-
STDERR.puts e.message
|
98
|
-
exit 1
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
data/lib/delayed/lifecycle.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
module Delayed
|
2
|
-
class InvalidCallback < Exception; end
|
3
|
-
|
4
|
-
class Lifecycle
|
5
|
-
EVENTS = {
|
6
|
-
:enqueue => [:job],
|
7
|
-
:execute => [:worker],
|
8
|
-
:loop => [:worker],
|
9
|
-
:perform => [:worker, :job],
|
10
|
-
:error => [:worker, :job],
|
11
|
-
:failure => [:worker, :job],
|
12
|
-
:invoke_job => [:job]
|
13
|
-
}
|
14
|
-
|
15
|
-
def initialize
|
16
|
-
@callbacks = EVENTS.keys.inject({}) { |hash, e| hash[e] = Callback.new; hash }
|
17
|
-
end
|
18
|
-
|
19
|
-
def before(event, &block)
|
20
|
-
add(:before, event, &block)
|
21
|
-
end
|
22
|
-
|
23
|
-
def after(event, &block)
|
24
|
-
add(:after, event, &block)
|
25
|
-
end
|
26
|
-
|
27
|
-
def around(event, &block)
|
28
|
-
add(:around, event, &block)
|
29
|
-
end
|
30
|
-
|
31
|
-
def run_callbacks(event, *args, &block)
|
32
|
-
missing_callback(event) unless @callbacks.has_key?(event)
|
33
|
-
|
34
|
-
unless EVENTS[event].size == args.size
|
35
|
-
raise ArgumentError, "Callback #{event} expects #{EVENTS[event].size} parameter(s): #{EVENTS[event].join(', ')}"
|
36
|
-
end
|
37
|
-
|
38
|
-
@callbacks[event].execute(*args, &block)
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def add(type, event, &block)
|
44
|
-
missing_callback(event) unless @callbacks.has_key?(event)
|
45
|
-
|
46
|
-
@callbacks[event].add(type, &block)
|
47
|
-
end
|
48
|
-
|
49
|
-
def missing_callback(event)
|
50
|
-
raise InvalidCallback, "Unknown callback event: #{event}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class Callback
|
55
|
-
def initialize
|
56
|
-
@before = []
|
57
|
-
@after = []
|
58
|
-
|
59
|
-
# Identity proc. Avoids special cases when there is no existing around chain.
|
60
|
-
@around = lambda { |*args, &block| block.call(*args) }
|
61
|
-
end
|
62
|
-
|
63
|
-
def execute(*args, &block)
|
64
|
-
@before.each { |c| c.call(*args) }
|
65
|
-
result = @around.call(*args, &block)
|
66
|
-
@after.each { |c| c.call(*args) }
|
67
|
-
result
|
68
|
-
end
|
69
|
-
|
70
|
-
def add(type, &callback)
|
71
|
-
case type
|
72
|
-
when :before
|
73
|
-
@before << callback
|
74
|
-
when :after
|
75
|
-
@after << callback
|
76
|
-
when :around
|
77
|
-
chain = @around # use a local variable so that the current chain is closed over in the following lambda
|
78
|
-
@around = lambda { |*a, &block| chain.call(*a) { |*b| callback.call(*b, &block) } }
|
79
|
-
else
|
80
|
-
raise InvalidCallback, "Invalid callback type: #{type}"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'active_support/basic_object'
|
2
|
-
require 'active_support/core_ext/module/aliasing'
|
3
|
-
|
4
|
-
module Delayed
|
5
|
-
class DelayProxy < ActiveSupport::BasicObject
|
6
|
-
def initialize(payload_class, target, options)
|
7
|
-
@payload_class = payload_class
|
8
|
-
@target = target
|
9
|
-
@options = options
|
10
|
-
end
|
11
|
-
|
12
|
-
def method_missing(method, *args)
|
13
|
-
Job.enqueue({:payload_object => @payload_class.new(@target, method.to_sym, args)}.merge(@options))
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
module MessageSending
|
18
|
-
def delay(options = {})
|
19
|
-
DelayProxy.new(PerformableMethod, self, options)
|
20
|
-
end
|
21
|
-
alias __delay__ delay
|
22
|
-
|
23
|
-
def send_later(method, *args)
|
24
|
-
warn "[DEPRECATION] `object.send_later(:method)` is deprecated. Use `object.delay.method"
|
25
|
-
__delay__.__send__(method, *args)
|
26
|
-
end
|
27
|
-
|
28
|
-
def send_at(time, method, *args)
|
29
|
-
warn "[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method"
|
30
|
-
__delay__(:run_at => time).__send__(method, *args)
|
31
|
-
end
|
32
|
-
|
33
|
-
module ClassMethods
|
34
|
-
def handle_asynchronously(method, opts = {})
|
35
|
-
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
|
36
|
-
with_method, without_method = "#{aliased_method}_with_delay#{punctuation}", "#{aliased_method}_without_delay#{punctuation}"
|
37
|
-
define_method(with_method) do |*args|
|
38
|
-
curr_opts = opts.clone
|
39
|
-
curr_opts.each_key do |key|
|
40
|
-
if (val = curr_opts[key]).is_a?(Proc)
|
41
|
-
curr_opts[key] = if val.arity == 1
|
42
|
-
val.call(self)
|
43
|
-
else
|
44
|
-
val.call
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
delay(curr_opts).__send__(without_method, *args)
|
49
|
-
end
|
50
|
-
alias_method_chain method, :delay
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'mail'
|
2
|
-
|
3
|
-
module Delayed
|
4
|
-
class PerformableMailer < PerformableMethod
|
5
|
-
def perform
|
6
|
-
object.send(method_name, *args).deliver
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
module DelayMail
|
11
|
-
def delay(options = {})
|
12
|
-
DelayProxy.new(PerformableMailer, self, options)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
Mail::Message.class_eval do
|
18
|
-
def delay(*args)
|
19
|
-
raise RuntimeError, "Use MyMailer.delay.mailer_action(args) to delay sending of emails."
|
20
|
-
end
|
21
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/module/delegation'
|
2
|
-
|
3
|
-
module Delayed
|
4
|
-
class PerformableMethod
|
5
|
-
attr_accessor :object, :method_name, :args
|
6
|
-
|
7
|
-
delegate :method, :to => :object
|
8
|
-
|
9
|
-
def initialize(object, method_name, args)
|
10
|
-
raise NoMethodError, "undefined method `#{method_name}' for #{object.inspect}" unless object.respond_to?(method_name, true)
|
11
|
-
|
12
|
-
self.object = object
|
13
|
-
self.args = args
|
14
|
-
self.method_name = method_name.to_sym
|
15
|
-
end
|
16
|
-
|
17
|
-
def display_name
|
18
|
-
"#{object.class}##{method_name}"
|
19
|
-
end
|
20
|
-
|
21
|
-
def perform
|
22
|
-
object.send(method_name, *args) if object
|
23
|
-
end
|
24
|
-
|
25
|
-
def method_missing(symbol, *args)
|
26
|
-
object.send(symbol, *args)
|
27
|
-
end
|
28
|
-
|
29
|
-
def respond_to?(symbol, include_private=false)
|
30
|
-
super || object.respond_to?(symbol, include_private)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/lib/delayed/plugin.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/class/attribute'
|
2
|
-
|
3
|
-
module Delayed
|
4
|
-
class Plugin
|
5
|
-
class_attribute :callback_block
|
6
|
-
|
7
|
-
def self.callbacks(&block)
|
8
|
-
self.callback_block = block
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
self.class.callback_block.call(Delayed::Worker.lifecycle) if self.class.callback_block
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Delayed
|
2
|
-
module Plugins
|
3
|
-
class ClearLocks < Plugin
|
4
|
-
callbacks do |lifecycle|
|
5
|
-
lifecycle.around(:execute) do |worker, &block|
|
6
|
-
begin
|
7
|
-
block.call(worker)
|
8
|
-
ensure
|
9
|
-
Delayed::Job.clear_locks!(worker.name)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/lib/delayed/psych_ext.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
if defined?(ActiveRecord)
|
2
|
-
class ActiveRecord::Base
|
3
|
-
# serialize to YAML
|
4
|
-
def encode_with(coder)
|
5
|
-
coder["attributes"] = @attributes
|
6
|
-
coder.tag = ['!ruby/ActiveRecord', self.class.name].join(':')
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class Delayed::PerformableMethod
|
12
|
-
# serialize to YAML
|
13
|
-
def encode_with(coder)
|
14
|
-
coder.map = {
|
15
|
-
"object" => object,
|
16
|
-
"method_name" => method_name,
|
17
|
-
"args" => args
|
18
|
-
}
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
module Psych
|
23
|
-
module Visitors
|
24
|
-
class YAMLTree
|
25
|
-
def visit_Class(klass)
|
26
|
-
tag = ['!ruby/class', klass.name].join(':')
|
27
|
-
register(klass, @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK))
|
28
|
-
@emitter.end_mapping
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class ToRuby
|
33
|
-
def visit_Psych_Nodes_Mapping_with_class(object)
|
34
|
-
return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag]
|
35
|
-
|
36
|
-
case object.tag
|
37
|
-
when /^!ruby\/class:?(.*)?$/
|
38
|
-
resolve_class $1
|
39
|
-
when /^!ruby\/ActiveRecord:(.+)$/
|
40
|
-
klass = resolve_class($1)
|
41
|
-
payload = Hash[*object.children.map { |c| accept c }]
|
42
|
-
id = payload["attributes"][klass.primary_key]
|
43
|
-
begin
|
44
|
-
if ActiveRecord::VERSION::MAJOR == 3
|
45
|
-
klass.unscoped.find(id)
|
46
|
-
else # Rails 2
|
47
|
-
klass.with_exclusive_scope { klass.find(id) }
|
48
|
-
end
|
49
|
-
rescue ActiveRecord::RecordNotFound
|
50
|
-
raise Delayed::DeserializationError
|
51
|
-
end
|
52
|
-
when /^!ruby\/Mongoid:(.+)$/
|
53
|
-
klass = resolve_class($1)
|
54
|
-
payload = Hash[*object.children.map { |c| accept c }]
|
55
|
-
begin
|
56
|
-
klass.find(payload["attributes"]["_id"])
|
57
|
-
rescue Mongoid::Errors::DocumentNotFound
|
58
|
-
raise Delayed::DeserializationError
|
59
|
-
end
|
60
|
-
else
|
61
|
-
visit_Psych_Nodes_Mapping_without_class(object)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
alias_method_chain :visit_Psych_Nodes_Mapping, :class
|
65
|
-
|
66
|
-
def resolve_class_with_constantize(klass_name)
|
67
|
-
klass_name.constantize
|
68
|
-
rescue
|
69
|
-
resolve_class_without_constantize(klass_name)
|
70
|
-
end
|
71
|
-
alias_method_chain :resolve_class, :constantize
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
data/lib/delayed/railtie.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'delayed_job'
|
2
|
-
require 'rails'
|
3
|
-
|
4
|
-
module Delayed
|
5
|
-
class Railtie < Rails::Railtie
|
6
|
-
initializer :after_initialize do
|
7
|
-
ActiveSupport.on_load(:action_mailer) do
|
8
|
-
ActionMailer::Base.send(:extend, Delayed::DelayMail)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
rake_tasks do
|
13
|
-
load 'delayed/tasks.rb'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/lib/delayed/recipes.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
# Capistrano Recipes for managing delayed_job
|
2
|
-
#
|
3
|
-
# Add these callbacks to have the delayed_job process restart when the server
|
4
|
-
# is restarted:
|
5
|
-
#
|
6
|
-
# after "deploy:stop", "delayed_job:stop"
|
7
|
-
# after "deploy:start", "delayed_job:start"
|
8
|
-
# after "deploy:restart", "delayed_job:restart"
|
9
|
-
#
|
10
|
-
# If you want to use command line options, for example to start multiple workers,
|
11
|
-
# define a Capistrano variable delayed_job_args:
|
12
|
-
#
|
13
|
-
# set :delayed_job_args, "-n 2"
|
14
|
-
#
|
15
|
-
# If you've got delayed_job workers running on a servers, you can also specify
|
16
|
-
# which servers have delayed_job running and should be restarted after deploy.
|
17
|
-
#
|
18
|
-
# set :delayed_job_server_role, :worker
|
19
|
-
#
|
20
|
-
|
21
|
-
Capistrano::Configuration.instance.load do
|
22
|
-
namespace :delayed_job do
|
23
|
-
def rails_env
|
24
|
-
fetch(:rails_env, false) ? "RAILS_ENV=#{fetch(:rails_env)}" : ''
|
25
|
-
end
|
26
|
-
|
27
|
-
def args
|
28
|
-
fetch(:delayed_job_args, "")
|
29
|
-
end
|
30
|
-
|
31
|
-
def roles
|
32
|
-
fetch(:delayed_job_server_role, :app)
|
33
|
-
end
|
34
|
-
|
35
|
-
desc "Stop the delayed_job process"
|
36
|
-
task :stop, :roles => lambda { roles } do
|
37
|
-
run "cd #{current_path};#{rails_env} script/delayed_job stop"
|
38
|
-
end
|
39
|
-
|
40
|
-
desc "Start the delayed_job process"
|
41
|
-
task :start, :roles => lambda { roles } do
|
42
|
-
run "cd #{current_path};#{rails_env} script/delayed_job start #{args}"
|
43
|
-
end
|
44
|
-
|
45
|
-
desc "Restart the delayed_job process"
|
46
|
-
task :restart, :roles => lambda { roles } do
|
47
|
-
run "cd #{current_path};#{rails_env} script/delayed_job restart #{args}"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|