delayed_job_on_steroids 1.7.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/database'
2
+
3
+ describe Delayed::Worker do
4
+ it "is singleton" do
5
+ Delayed::Worker.included_modules.include?(Singleton).should == true
6
+ end
7
+
8
+ it "responds to methods moved from Job" do
9
+ Delayed::JobDeprecations.instance_methods.each do |m|
10
+ m = m.to_s.gsub("worker_", "").to_sym
11
+ Delayed::Worker.respond_to?(m).should == true
12
+ Delayed::Worker.instance.respond_to?(m).should == true
13
+ end
14
+ end
15
+ end
data/tasks/jobs.rake CHANGED
@@ -1 +1 @@
1
- require File.join(File.dirname(__FILE__), 'tasks')
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'delayed_on_steroids', 'tasks'))
metadata CHANGED
@@ -3,10 +3,10 @@ name: delayed_job_on_steroids
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 1
7
- - 7
8
- - 5
9
- version: 1.7.5
6
+ - 2
7
+ - 0
8
+ - 0
9
+ version: 2.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Tobias L\xC3\xBCtke"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-15 00:00:00 +03:00
18
+ date: 2010-03-24 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -39,28 +39,33 @@ executables: []
39
39
  extensions: []
40
40
 
41
41
  extra_rdoc_files:
42
- - README.textile
42
+ - README.markdown
43
43
  files:
44
44
  - .gitignore
45
45
  - MIT-LICENSE
46
- - README.textile
46
+ - README.markdown
47
47
  - Rakefile
48
48
  - VERSION
49
49
  - delayed_job_on_steroids.gemspec
50
+ - generators/delayed_job/delayed_job_generator.rb
51
+ - generators/delayed_job/templates/script
50
52
  - generators/delayed_job_migration/delayed_job_migration_generator.rb
51
53
  - generators/delayed_job_migration/templates/migration.rb
52
54
  - init.rb
53
- - lib/delayed/job.rb
54
- - lib/delayed/message_sending.rb
55
- - lib/delayed/performable_method.rb
56
- - lib/delayed/worker.rb
57
- - lib/delayed_job.rb
55
+ - lib/delayed_job_on_steroids.rb
56
+ - lib/delayed_on_steroids/command.rb
57
+ - lib/delayed_on_steroids/job.rb
58
+ - lib/delayed_on_steroids/job_deprecations.rb
59
+ - lib/delayed_on_steroids/message_sending.rb
60
+ - lib/delayed_on_steroids/performable_method.rb
61
+ - lib/delayed_on_steroids/tasks.rb
62
+ - lib/delayed_on_steroids/worker.rb
58
63
  - spec/database.rb
59
64
  - spec/delayed_method_spec.rb
60
65
  - spec/job_spec.rb
61
66
  - spec/story_spec.rb
67
+ - spec/worker_spec.rb
62
68
  - tasks/jobs.rake
63
- - tasks/tasks.rb
64
69
  has_rdoc: true
65
70
  homepage: http://github.com/AlekSi/delayed_job_on_steroids
66
71
  licenses: []
@@ -96,3 +101,4 @@ test_files:
96
101
  - spec/job_spec.rb
97
102
  - spec/story_spec.rb
98
103
  - spec/database.rb
104
+ - spec/worker_spec.rb
data/README.textile DELETED
@@ -1,129 +0,0 @@
1
- h1. Delayed::Job
2
-
3
- Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background.
4
-
5
- It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks. Amongst those tasks are:
6
-
7
- * sending massive newsletters
8
- * image resizing
9
- * http downloads
10
- * updating smart collections
11
- * updating solr, our search server, after product changes
12
- * batch imports
13
- * spam checks
14
-
15
- h2. Setup
16
-
17
- The library evolves around a delayed_jobs table which can be created by using:
18
- <pre><code>
19
- script/generate delayed_job_migration
20
- </code></pre>
21
-
22
- The created table looks as follows:
23
-
24
- <pre><code>
25
- create_table :delayed_jobs, :force => true do |table|
26
- table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
27
- table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
28
- table.text :handler # YAML-encoded string of the object that will do work
29
- table.string :job_type # Class name of the job object, for type-specific workers
30
- table.string :last_error # reason for last failure (See Note below)
31
- table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future.
32
- table.datetime :locked_at # Set when a client is working on this object
33
- table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
34
- table.string :locked_by # Who is working on this object (if locked)
35
- table.timestamps
36
- end
37
- </code></pre>
38
-
39
- On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries.
40
-
41
- The default @MAX_ATTEMPTS@ is @25@. After this, the job either deleted (default), or left in the database with "failed_at" set.
42
- With the default of 25 attempts, the last retry will be 20 days later, with the last interval being almost 100 hours.
43
-
44
- The default @MAX_RUN_TIME@ is @4.hours@. If your job takes longer than that, another computer could pick it up. It's up to you to
45
- make sure your job doesn't exceed this time. You should set this to the longest time you think the job could take.
46
-
47
- By default, it will delete failed jobs (and it always deletes successful jobs). If you want to keep failed jobs, set
48
- @Delayed::Job.destroy_failed_jobs = false@. The failed jobs will be marked with non-null failed_at.
49
-
50
- Here is an example of changing job parameters in Rails:
51
-
52
- <pre><code>
53
- # config/initializers/delayed_job_config.rb
54
- Delayed::Job.destroy_failed_jobs = false
55
- silence_warnings do
56
- Delayed::Job.const_set("MAX_ATTEMPTS", 3)
57
- Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)
58
- end
59
- </code></pre>
60
-
61
- Note: If your error messages are long, consider changing last_error field to a :text instead of a :string (255 character limit).
62
-
63
-
64
- h2. Usage
65
-
66
- Jobs are simple ruby objects with a method called perform. Any object which responds to perform can be stuffed into the jobs table.
67
- Job objects are serialized to yaml so that they can later be resurrected by the job runner.
68
-
69
- <pre><code>
70
- class NewsletterJob < Struct.new(:text, :emails)
71
- def perform
72
- emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
73
- end
74
- end
75
-
76
- Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
77
- </code></pre>
78
-
79
- There is also a second way to get jobs in the queue: send_later.
80
-
81
- <pre><code>
82
- BatchImporter.new(Shop.find(1)).send_later(:import_massive_csv, massive_csv)
83
- </code></pre>
84
-
85
- This will simply create a @Delayed::PerformableMethod@ job in the jobs table which serializes all the parameters you pass to it. There are some special smarts for active record objects
86
- which are stored as their text representation and loaded from the database fresh when the job is actually run later.
87
-
88
-
89
- h2. Running the jobs
90
-
91
- You can invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
92
-
93
- You can also run by writing a simple @script/job_runner@, and invoking it externally:
94
-
95
- <pre><code>
96
- #!/usr/bin/env ruby
97
- require File.dirname(__FILE__) + '/../config/environment'
98
-
99
- Delayed::Worker.new.start
100
- </code></pre>
101
-
102
- Workers can be running on any computer, as long as they have access to the database and their clock is in sync. You can even
103
- run multiple workers on per computer, but you must give each one a unique name. (TODO: put in an example)
104
- Keep in mind that each worker will check the database at least every 5 seconds.
105
-
106
- Note: The rake task will exit if the database has any network connectivity problems.
107
-
108
- If you only want to run specific types of jobs in a given worker, include them when initializing the worker:
109
-
110
- <pre><code>
111
- Delayed::Worker.new(:job_types => "SimpleJob").start
112
- Delayed::Worker.new(:job_types => ["SimpleJob", "NewsletterJob"]).start
113
- </pre></code>
114
-
115
- h3. Cleaning up
116
-
117
- You can invoke @rake jobs:clear@ to delete all jobs in the queue.
118
-
119
- h3. Changes
120
-
121
- * 1.7.0: Added failed_at column which can optionally be set after a certain amount of failed job attempts. By default failed job attempts are destroyed after about a month.
122
-
123
- * 1.6.0: Renamed locked_until to locked_at. We now store when we start a given job instead of how long it will be locked by the worker. This allows us to get a reading on how long a job took to execute.
124
-
125
- * 1.5.0: Job runners can now be run in parallel. Two new database columns are needed: locked_until and locked_by. This allows us to use pessimistic locking instead of relying on row level locks. This enables us to run as many worker processes as we need to speed up queue processing.
126
-
127
- * 1.2.0: Added #send_later to Object for simpler job creation
128
-
129
- * 1.0.0: Initial release
@@ -1,55 +0,0 @@
1
- module Delayed
2
- class Worker
3
- SLEEP = 5
4
-
5
- cattr_accessor :logger
6
- self.logger = if defined?(Merb::Logger)
7
- Merb.logger
8
- elsif defined?(RAILS_DEFAULT_LOGGER)
9
- RAILS_DEFAULT_LOGGER
10
- end
11
-
12
- def initialize(options={})
13
- @quiet = options[:quiet]
14
- Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
15
- Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
16
- Delayed::Job.job_types = options[:job_types] if options.has_key?(:job_types)
17
- end
18
-
19
- def start
20
- say "*** Starting job worker #{Delayed::Job.worker_name}"
21
-
22
- trap('TERM') { say 'Exiting...'; $exit = true }
23
- trap('INT') { say 'Exiting...'; $exit = true }
24
-
25
- loop do
26
- result = nil
27
-
28
- realtime = Benchmark.realtime do
29
- result = Delayed::Job.work_off
30
- end
31
-
32
- count = result.sum
33
-
34
- break if $exit
35
-
36
- if count.zero?
37
- sleep(SLEEP)
38
- else
39
- say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
40
- end
41
-
42
- break if $exit
43
- end
44
-
45
- ensure
46
- Delayed::Job.clear_locks!
47
- end
48
-
49
- def say(text)
50
- puts text unless @quiet
51
- logger.info text if logger
52
- end
53
-
54
- end
55
- end
data/lib/delayed_job.rb DELETED
@@ -1,13 +0,0 @@
1
- autoload :ActiveRecord, 'activerecord'
2
-
3
- require File.dirname(__FILE__) + '/delayed/message_sending'
4
- require File.dirname(__FILE__) + '/delayed/performable_method'
5
- require File.dirname(__FILE__) + '/delayed/job'
6
- require File.dirname(__FILE__) + '/delayed/worker'
7
-
8
- Object.send(:include, Delayed::MessageSending)
9
- Module.send(:include, Delayed::MessageSending::ClassMethods)
10
-
11
- if defined?(Merb::Plugins)
12
- Merb::Plugins.add_rakefiles File.dirname(__FILE__) / '..' / 'tasks' / 'tasks'
13
- end
data/tasks/tasks.rb DELETED
@@ -1,16 +0,0 @@
1
- # Re-definitions are appended to existing tasks
2
- task :environment
3
- task :merb_env
4
-
5
- namespace :jobs do
6
- desc "Clear the delayed_job queue."
7
- task :clear => [:merb_env, :environment] do
8
- Delayed::Job.delete_all
9
- end
10
-
11
- desc "Start a delayed_job worker (options: MIN_PRIORITY, MAX_PRIORITY, JOB_TYPES)."
12
- task :work => [:merb_env, :environment] do
13
- Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'],
14
- :job_types => ENV['JOB_TYPES']).start
15
- end
16
- end