diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 59c456285a..bde177f8c9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -137,7 +137,7 @@ def deny_access # Authorize the user for the requested action def authorize(ctrl = params[:controller], action = params[:action], global = false) - logger.info("entering authorize") + # logger.info("entering authorize") allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) allowed ? true : deny_access end diff --git a/app/models/document_observer.rb b/app/models/document_observer.rb index f51e55b354..36011c55dd 100644 --- a/app/models/document_observer.rb +++ b/app/models/document_observer.rb @@ -4,6 +4,6 @@ class DocumentObserver < ActiveRecord::Observer def after_create(document) - Mailer.deliver_document_added(document) if Setting.notified_events.include?('document_added') + Mailer.send_later(:deliver_document_added,document) if Setting.notified_events.include?('document_added') end end diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb index c3a027c5ae..bc6e9f0a7e 100644 --- a/app/models/issue_observer.rb +++ b/app/models/issue_observer.rb @@ -4,6 +4,6 @@ class IssueObserver < ActiveRecord::Observer def after_create(issue) - Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added') + Mailer.send_later(:deliver_issue_add,issue) if Setting.notified_events.include?('issue_added') end end diff --git a/app/models/journal_observer.rb b/app/models/journal_observer.rb index 0bd85b87a9..47180d97ac 100644 --- a/app/models/journal_observer.rb +++ b/app/models/journal_observer.rb @@ -4,6 +4,6 @@ class JournalObserver < ActiveRecord::Observer def after_create(journal) - Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') + Mailer.send_later(:deliver_issue_edit,journal) if Setting.notified_events.include?('issue_updated') end end diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 620073b682..3493334b74 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -1,19 +1,6 @@ # BetterMeans - Work 2.0 # Copyright (C) 2009 Shereef Bishay # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class Mailer < ActionMailer::Base layout 'mailer' @@ -285,6 +272,7 @@ def deliver!(mail = @mail) end super(mail) end + # Sends reminders to issue assignees # Available options: diff --git a/app/models/message_observer.rb b/app/models/message_observer.rb index 685fcdbf1b..e44b3de364 100644 --- a/app/models/message_observer.rb +++ b/app/models/message_observer.rb @@ -4,6 +4,6 @@ class MessageObserver < ActiveRecord::Observer def after_create(message) - Mailer.deliver_message_posted(message) if Setting.notified_events.include?('message_posted') + Mailer.send_later(:deliver_message_posted,message) if Setting.notified_events.include?('message_posted') end end diff --git a/app/models/news_observer.rb b/app/models/news_observer.rb index b8aaf1f1d0..cec20b41bc 100644 --- a/app/models/news_observer.rb +++ b/app/models/news_observer.rb @@ -4,6 +4,6 @@ class NewsObserver < ActiveRecord::Observer def after_create(news) - Mailer.deliver_news_added(news) if Setting.notified_events.include?('news_added') + Mailer.send_later(:deliver_news_added,news) if Setting.notified_events.include?('news_added') end end diff --git a/db/migrate/20100109014849_create_delayed_jobs.rb b/db/migrate/20100109014849_create_delayed_jobs.rb deleted file mode 100644 index 1bbdc06d33..0000000000 --- a/db/migrate/20100109014849_create_delayed_jobs.rb +++ /dev/null @@ -1,18 +0,0 @@ -class CreateDelayedJobs < ActiveRecord::Migration -def self.up - create_table :delayed_jobs, :force => true do |table| - table.integer :priority, :default => 0 # jobs can jump to the front of - table.integer :attempts, :default => 0 # retries, but still fail eventually - table.text :handler # YAML object dump - table.text :last_error # last failure - table.datetime :run_at # schedule for later - table.datetime :locked_at # set when client working this job - table.datetime :failed_at # set when all retries have failed - table.text :locked_by # who is working on this object - table.timestamps - end - end - def self.down - drop_table :delayed_jobs - end -end \ No newline at end of file diff --git a/db/migrate/20100109023733_create_delayed_jobs.rb b/db/migrate/20100109023733_create_delayed_jobs.rb new file mode 100644 index 0000000000..4471f7b47b --- /dev/null +++ b/db/migrate/20100109023733_create_delayed_jobs.rb @@ -0,0 +1,20 @@ +class CreateDelayedJobs < ActiveRecord::Migration + def self.up + create_table :delayed_jobs, :force => true do |table| + table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue + table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually. + table.text :handler # YAML-encoded string of the object that will do work + table.text :last_error # reason for last failure (See Note below) + table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. + table.datetime :locked_at # Set when a client is working on this object + table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) + table.string :locked_by # Who is working on this object (if locked) + table.timestamps + end + + end + + def self.down + drop_table :delayed_jobs + end +end \ No newline at end of file diff --git a/script/delayed_job b/script/delayed_job new file mode 100755 index 0000000000..edf195985f --- /dev/null +++ b/script/delayed_job @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize diff --git a/vendor/plugins/delayed_job/README.textile b/vendor/plugins/delayed_job/README.textile index e56ef3edf3..2a76ed2fb0 100644 --- a/vendor/plugins/delayed_job/README.textile +++ b/vendor/plugins/delayed_job/README.textile @@ -1,6 +1,6 @@ h1. Delayed::Job -Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. +Delated_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks. Amongst those tasks are: @@ -12,101 +12,163 @@ It is a direct extraction from Shopify where the job table is responsible for a * batch imports * spam checks -h2. Setup +h2. Installation -The library evolves around a delayed_jobs table which can be created by using: -

-  script/generate delayed_job_migration
-
+To install as a gem, add the following to @config/environment.rb@: -The created table looks as follows: +
+config.gem 'collectiveidea-delayed_job', :lib => 'delayed_job',
+  :source => 'http://gems.github.com'
+
+ +Rake tasks are not automatically loaded from gems, so you'll need to add the following to your Rakefile: + +
+begin
+  require 'delayed/tasks'
+rescue LoadError
+  STDERR.puts "Run `rake gems:install` to install delayed_job"
+end
+
+ +To install as a plugin: + +
+script/plugin install git://github.com/collectiveidea/delayed_job.git
+
+ +After delayed_job is installed, run: + +
+script/generate delayed_job
+rake db:migrate
+
+ +h2. Upgrading to 1.8 + +If you are upgrading from a previous release, you will need to generate the new @script/delayed_job@: + +
+script/generate delayed_job --skip-migration
+
+ +h2. Queuing Jobs + +Call @#send_later(method, params)@ on any object and it will be processed in the background. + +
+# without delayed_job
+Notifier.deliver_signup(@user)
+
+# with delayed_job
+Notifier.send_later :deliver_signup, @user
+
+ +If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration: + +
+class Device
+  def deliver
+    # long running method
+  end
+  handle_asynchronously :deliver
+end
+
+device = Device.new
+device.deliver
+
+ +h2. Running Jobs + +@script/delayed_job@ can be used to manage a background process which will start working off jobs. + +
+$ RAILS_ENV=production script/delayed_job start
+$ RAILS_ENV=production script/delayed_job stop
+
+# Runs two workers in separate processes.
+$ RAILS_ENV=production script/delayed_job -n 2 start
+$ RAILS_ENV=production script/delayed_job stop
+
+ +Workers can be running on any computer, as long as they have access to the database and their clock is in sync. Keep in mind that each worker will check the database at least every 5 seconds. + +You can also invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@. + +h2. Custom Jobs + +Jobs are simple ruby objects with a method called perform. Any object which responds to perform can be stuffed into the jobs table. Job objects are serialized to yaml so that they can later be resurrected by the job runner. + +
+class NewsletterJob < Struct.new(:text, :emails)
+  def perform
+    emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
+  end    
+end  
+  
+Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
+
+ +h2. Gory Details + +The library evolves around a delayed_jobs table which looks as follows: -

   create_table :delayed_jobs, :force => true do |table|
     table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
     table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
     table.text     :handler                      # YAML-encoded string of the object that will do work
-    table.string   :last_error                   # reason for last failure (See Note below)
-    table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.
+    table.text   :last_error                   # reason for last failure (See Note below)
+    table.datetime :run_at                       # When to run. Could be Time.zone.now for immediately, or sometime in the future.
     table.datetime :locked_at                    # Set when a client is working on this object
     table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)
     table.string   :locked_by                    # Who is working on this object (if locked)
     table.timestamps
   end
-
On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries. -The default @MAX_ATTEMPTS@ is @25@. After this, the job either deleted (default), or left in the database with "failed_at" set. +The default Worker.max_attempts is 25. After this, the job either deleted (default), or left in the database with "failed_at" set. With the default of 25 attempts, the last retry will be 20 days later, with the last interval being almost 100 hours. -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 +The default Worker.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 make sure your job doesn't exceed this time. You should set this to the longest time you think the job could take. By default, it will delete failed jobs (and it always deletes successful jobs). If you want to keep failed jobs, set -@Delayed::Job.destroy_failed_jobs = false@. The failed jobs will be marked with non-null failed_at. +Delayed::Worker.destroy_failed_jobs = false. The failed jobs will be marked with non-null failed_at. Here is an example of changing job parameters in Rails: -

-  # config/initializers/delayed_job_config.rb
-  Delayed::Job.destroy_failed_jobs = false
-  silence_warnings do
-    Delayed::Job.const_set("MAX_ATTEMPTS", 3)
-    Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)
-  end
-
- -Note: If your error messages are long, consider changing last_error field to a :text instead of a :string (255 character limit). - +
+# config/initializers/delayed_job_config.rb
+Delayed::Worker.destroy_failed_jobs = false
+Delayed::Worker.sleep_delay = 60
+Delayed::Worker.max_attempts = 3
+Delayed::Worker.max_run_time = 5.minutes
+
-h2. Usage - -Jobs are simple ruby objects with a method called perform. Any object which responds to perform can be stuffed into the jobs table. -Job objects are serialized to yaml so that they can later be resurrected by the job runner. - -

-  class NewsletterJob < Struct.new(:text, :emails)
-    def perform
-      emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
-    end    
-  end  
-  
-  Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
-
- -There is also a second way to get jobs in the queue: send_later. +h3. Cleaning up -

-  BatchImporter.new(Shop.find(1)).send_later(:import_massive_csv, massive_csv)
-
+You can invoke @rake jobs:clear@ to delete all jobs in the queue. -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 -which are stored as their text representation and loaded from the database fresh when the job is actually run later. - - -h2. Running the jobs +h2. Mailing List -You can invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@. +Join us on the mailing list at http://groups.google.com/group/delayed_job -You can also run by writing a simple @script/job_runner@, and invoking it externally: - -

-  #!/usr/bin/env ruby
-  require File.dirname(__FILE__) + '/../config/environment'
-  
-  Delayed::Worker.new.start  
-
+h2. How to contribute -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 -run multiple workers on per computer, but you must give each one a unique name. (TODO: put in an example) -Keep in mind that each worker will check the database at least every 5 seconds. +If you find what looks like a bug: -Note: The rake task will exit if the database has any network connectivity problems. +# Check the GitHub issue tracker to see if anyone else has had the same issue. + http://github.com/collectiveidea/delayed_job/issues/ +# If you don't see anything, create an issue with information on how to reproduce it. -h3. Cleaning up +If you want to contribute an enhancement or a fix: -You can invoke @rake jobs:clear@ to delete all jobs in the queue. +# Fork the project on github. + http://github.com/collectiveidea/delayed_job/ +# Make your changes with tests. +# Commit the changes without making changes to the Rakefile, VERSION, or any other files that aren't related to your enhancement or fix +# Send a pull request. h3. Changes diff --git a/vendor/plugins/delayed_job/Rakefile b/vendor/plugins/delayed_job/Rakefile new file mode 100644 index 0000000000..f9257421fb --- /dev/null +++ b/vendor/plugins/delayed_job/Rakefile @@ -0,0 +1,38 @@ +# -*- encoding: utf-8 -*- +begin + require 'jeweler' +rescue LoadError + puts "Jeweler not available. Install it with: sudo gem install jeweler" + exit 1 +end + +Jeweler::Tasks.new do |s| + s.name = "delayed_job" + s.summary = "Database-backed asynchronous priority queue system -- Extracted from Shopify" + s.email = "tobi@leetsoft.com" + s.homepage = "http://github.com/collectiveidea/delayed_job" + s.description = "Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks." + s.authors = ["Brandon Keepers", "Tobias Lütke"] + + s.has_rdoc = true + s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"] + s.extra_rdoc_files = ["README.textile"] + + s.test_files = Dir['spec/**/*'] + + s.add_development_dependency "rspec" + s.add_development_dependency "sqlite3-ruby" +end + +require 'spec/rake/spectask' + +task :default => :spec + +desc 'Run the specs' +Spec::Rake::SpecTask.new(:spec) do |t| + t.libs << 'lib' + t.pattern = 'spec/**/*_spec.rb' + t.verbose = true +end +task :spec => :check_dependencies + diff --git a/vendor/plugins/delayed_job/VERSION b/vendor/plugins/delayed_job/VERSION new file mode 100644 index 0000000000..bfa363e76e --- /dev/null +++ b/vendor/plugins/delayed_job/VERSION @@ -0,0 +1 @@ +1.8.4 diff --git a/vendor/plugins/delayed_job/contrib/delayed_job.monitrc b/vendor/plugins/delayed_job/contrib/delayed_job.monitrc new file mode 100644 index 0000000000..e9e0c4432b --- /dev/null +++ b/vendor/plugins/delayed_job/contrib/delayed_job.monitrc @@ -0,0 +1,14 @@ +# an example Monit configuration file for delayed_job +# See: http://stackoverflow.com/questions/1226302/how-to-monitor-delayedjob-with-monit/1285611 +# +# To use: +# 1. copy to /var/www/apps/{app_name}/shared/delayed_job.monitrc +# 2. replace {app_name} as appropriate +# 3. add this to your /etc/monit/monitrc +# +# include /var/www/apps/{app_name}/shared/delayed_job.monitrc + +check process delayed_job + with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.pid + start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start" + stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job stop" \ No newline at end of file diff --git a/vendor/plugins/delayed_job/delayed_job.gemspec b/vendor/plugins/delayed_job/delayed_job.gemspec index fd2fddf46e..b97dc7ed7d 100644 --- a/vendor/plugins/delayed_job/delayed_job.gemspec +++ b/vendor/plugins/delayed_job/delayed_job.gemspec @@ -1,41 +1,66 @@ -#version = File.read('README.textile').scan(/^\*\s+([\d\.]+)/).flatten +# Generated by jeweler +# DO NOT EDIT THIS FILE +# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` +# -*- encoding: utf-8 -*- Gem::Specification.new do |s| - s.name = "delayed_job" - s.version = "1.7.0" - s.date = "2008-11-28" - s.summary = "Database-backed asynchronous priority queue system -- Extracted from Shopify" - s.email = "tobi@leetsoft.com" - s.homepage = "http://github.com/tobi/delayed_job/tree/master" - s.description = "Delated_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks." - s.authors = ["Tobias Lütke"] + s.name = %q{delayed_job} + s.version = "1.8.4" - # s.bindir = "bin" - # s.executables = ["delayed_job"] - # s.default_executable = "delayed_job" - - s.has_rdoc = false - s.rdoc_options = ["--main", "README.textile"] - s.extra_rdoc_files = ["README.textile"] - - # run git ls-files to get an updated list - s.files = %w[ - MIT-LICENSE - README.textile - delayed_job.gemspec - init.rb - lib/delayed/job.rb - lib/delayed/message_sending.rb - lib/delayed/performable_method.rb - lib/delayed/worker.rb - lib/delayed_job.rb - tasks/jobs.rake - tasks/tasks.rb + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Brandon Keepers", "Tobias L\303\274tke"] + s.date = %q{2009-10-06} + s.description = %q{Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks.} + s.email = %q{tobi@leetsoft.com} + s.extra_rdoc_files = [ + "README.textile" + ] + s.files = [ + ".gitignore", + "MIT-LICENSE", + "README.textile", + "Rakefile", + "VERSION", + "contrib/delayed_job.monitrc", + "delayed_job.gemspec", + "generators/delayed_job/delayed_job_generator.rb", + "generators/delayed_job/templates/migration.rb", + "generators/delayed_job/templates/script", + "init.rb", + "lib/delayed/command.rb", + "lib/delayed/job.rb", + "lib/delayed/message_sending.rb", + "lib/delayed/performable_method.rb", + "lib/delayed/recipes.rb", + "lib/delayed/tasks.rb", + "lib/delayed/worker.rb", + "lib/delayed_job.rb", + "recipes/delayed_job.rb", + "spec/database.rb", + "spec/delayed_method_spec.rb", + "spec/job_spec.rb", + "spec/story_spec.rb", + "tasks/jobs.rake" ] - s.test_files = %w[ - spec/database.rb - spec/delayed_method_spec.rb - spec/job_spec.rb - spec/story_spec.rb + s.homepage = %q{http://github.com/collectiveidea/delayed_job} + s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.5} + s.summary = %q{Database-backed asynchronous priority queue system -- Extracted from Shopify} + s.test_files = [ + "spec/database.rb", + "spec/delayed_method_spec.rb", + "spec/job_spec.rb", + "spec/story_spec.rb" ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + else + end + else + end end diff --git a/vendor/plugins/delayed_job/generators/delayed_job/delayed_job_generator.rb b/vendor/plugins/delayed_job/generators/delayed_job/delayed_job_generator.rb new file mode 100644 index 0000000000..5e931b783f --- /dev/null +++ b/vendor/plugins/delayed_job/generators/delayed_job/delayed_job_generator.rb @@ -0,0 +1,22 @@ +class DelayedJobGenerator < Rails::Generator::Base + default_options :skip_migration => false + + def manifest + record do |m| + m.template 'script', 'script/delayed_job', :chmod => 0755 + unless options[:skip_migration] + m.migration_template "migration.rb", 'db/migrate', + :migration_file_name => "create_delayed_jobs" + end + end + end + +protected + + def add_options!(opt) + opt.separator '' + opt.separator 'Options:' + opt.on("--skip-migration", "Don't generate a migration") { |v| options[:skip_migration] = v } + end + +end diff --git a/vendor/plugins/delayed_job/generators/delayed_job/templates/migration.rb b/vendor/plugins/delayed_job/generators/delayed_job/templates/migration.rb new file mode 100644 index 0000000000..4471f7b47b --- /dev/null +++ b/vendor/plugins/delayed_job/generators/delayed_job/templates/migration.rb @@ -0,0 +1,20 @@ +class CreateDelayedJobs < ActiveRecord::Migration + def self.up + create_table :delayed_jobs, :force => true do |table| + table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue + table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually. + table.text :handler # YAML-encoded string of the object that will do work + table.text :last_error # reason for last failure (See Note below) + table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. + table.datetime :locked_at # Set when a client is working on this object + table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) + table.string :locked_by # Who is working on this object (if locked) + table.timestamps + end + + end + + def self.down + drop_table :delayed_jobs + end +end \ No newline at end of file diff --git a/vendor/plugins/delayed_job/generators/delayed_job/templates/script b/vendor/plugins/delayed_job/generators/delayed_job/templates/script new file mode 100755 index 0000000000..edf195985f --- /dev/null +++ b/vendor/plugins/delayed_job/generators/delayed_job/templates/script @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize diff --git a/vendor/plugins/delayed_job/generators/delayed_job_migration/delayed_job_migration_generator.rb b/vendor/plugins/delayed_job/generators/delayed_job_migration/delayed_job_migration_generator.rb deleted file mode 100644 index 5822350fcf..0000000000 --- a/vendor/plugins/delayed_job/generators/delayed_job_migration/delayed_job_migration_generator.rb +++ /dev/null @@ -1,15 +0,0 @@ -class DelayedJobMigrationGenerator < Rails::Generator::Base - def manifest - record do |m| - options = { - :migration_file_name => 'create_delayed_jobs' - } - - m.migration_template 'migration.rb', 'db/migrate', options - end - end - - def banner - "Usage: #{$0} #{spec.name}" - end -end diff --git a/vendor/plugins/delayed_job/generators/delayed_job_migration/templates/migration.rb b/vendor/plugins/delayed_job/generators/delayed_job_migration/templates/migration.rb deleted file mode 100644 index c7e5bb4c11..0000000000 --- a/vendor/plugins/delayed_job/generators/delayed_job_migration/templates/migration.rb +++ /dev/null @@ -1,22 +0,0 @@ -class CreateDelayedJobs < ActiveRecord::Migration - def self.up - create_table :delayed_jobs, :force => true do |t| - t.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue - t.integer :attempts, :default => 0 # Provides for retries, but still fail eventually. - t.text :handler # YAML-encoded string of the object that will do work - t.string :last_error # reason for last failure (See Note below) - t.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future. - t.datetime :locked_at # Set when a client is working on this object - t.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) - t.string :locked_by # Who is working on this object (if locked) - - t.timestamps - end - - add_index :delayed_jobs, :locked_by - end - - def self.down - drop_table :delayed_jobs - end -end diff --git a/vendor/plugins/delayed_job/lib/delayed/command.rb b/vendor/plugins/delayed_job/lib/delayed/command.rb new file mode 100644 index 0000000000..93eff14e4c --- /dev/null +++ b/vendor/plugins/delayed_job/lib/delayed/command.rb @@ -0,0 +1,76 @@ +require 'rubygems' +require 'daemons' +require 'optparse' + +module Delayed + class Command + attr_accessor :worker_count + + def initialize(args) + @files_to_reopen = [] + @options = {:quiet => true} + + @worker_count = 1 + + opts = OptionParser.new do |opts| + opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run" + + opts.on('-h', '--help', 'Show this message') do + puts opts + exit 1 + end + opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e| + 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" + end + opts.on('--min-priority N', 'Minimum priority of jobs to run.') do |n| + @options[:min_priority] = n + end + opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n| + @options[:max_priority] = n + end + opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count| + @worker_count = worker_count.to_i rescue 1 + end + end + @args = opts.parse!(args) + end + + def daemonize + ObjectSpace.each_object(File) do |file| + @files_to_reopen << file unless file.closed? + end + + worker_count.times do |worker_index| + process_name = worker_count == 1 ? "delayed_job" : "delayed_job.#{worker_index}" + Daemons.run_proc(process_name, :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args| + run process_name + end + end + end + + def run(worker_name = nil) + Dir.chdir(RAILS_ROOT) + + # Re-open file handles + @files_to_reopen.each do |file| + begin + file.reopen File.join(RAILS_ROOT, 'log', 'delayed_job.log'), 'a+' + file.sync = true + rescue ::Exception + end + end + + Delayed::Worker.logger = Rails.logger + ActiveRecord::Base.connection.reconnect! + + worker = Delayed::Worker.new(@options) + worker.name_prefix = "#{worker_name} " + worker.start + rescue => e + Rails.logger.fatal e + STDERR.puts e.message + exit 1 + end + + end +end diff --git a/vendor/plugins/delayed_job/lib/delayed/job.rb b/vendor/plugins/delayed_job/lib/delayed/job.rb index 2dfa0bba19..c70c7e2558 100644 --- a/vendor/plugins/delayed_job/lib/delayed/job.rb +++ b/vendor/plugins/delayed_job/lib/delayed/job.rb @@ -1,3 +1,5 @@ +require 'timeout' + module Delayed class DeserializationError < StandardError @@ -6,33 +8,17 @@ class DeserializationError < StandardError # A job object that is persisted to the database. # Contains the work object as a YAML field. class Job < ActiveRecord::Base - MAX_ATTEMPTS = 25 - MAX_RUN_TIME = 4.hours set_table_name :delayed_jobs - # By default failed jobs are destroyed after too many attempts. - # If you want to keep them around (perhaps to inspect the reason - # for the failure), set this to false. - cattr_accessor :destroy_failed_jobs - self.destroy_failed_jobs = true - - # Every worker has a unique name which by default is the pid of the process. - # There are some advantages to overriding this with something which survives worker retarts: - # Workers can safely resume working on tasks which are locked by themselves. The worker will assume that it crashed before. - cattr_accessor :worker_name - self.worker_name = "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}" - - NextTaskSQL = '(run_at <= ? AND (locked_at IS NULL OR locked_at < ?) OR (locked_by = ?)) AND failed_at IS NULL' - NextTaskOrder = 'priority DESC, run_at ASC' + named_scope :ready_to_run, lambda {|worker_name, max_run_time| + {:conditions => ['(run_at <= ? AND (locked_at IS NULL OR locked_at < ?) OR locked_by = ?) AND failed_at IS NULL', db_time_now, db_time_now - max_run_time, worker_name]} + } + named_scope :by_priority, :order => 'priority DESC, run_at ASC' ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/ - cattr_accessor :min_priority, :max_priority - self.min_priority = nil - self.max_priority = nil - # When a worker is exiting, make sure we don't have any locked jobs. - def self.clear_locks! + def self.clear_locks!(worker_name) update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name]) end @@ -60,53 +46,10 @@ def payload_object=(object) self['handler'] = object.to_yaml end - # Reschedule the job in the future (when a job fails). - # Uses an exponential scale depending on the number of failed attempts. - def reschedule(message, backtrace = [], time = nil) - if self.attempts < MAX_ATTEMPTS - time ||= Job.db_time_now + (attempts ** 4) + 5 - - self.attempts += 1 - self.run_at = time - self.last_error = message + "\n" + backtrace.join("\n") - self.unlock - save! - else - logger.info "* [JOB] PERMANENTLY removing #{self.name} because of #{attempts} consequetive failures." - destroy_failed_jobs ? destroy : update_attribute(:failed_at, Delayed::Job.db_time_now) - end - end - - - # Try to run one job. Returns true/false (work done/work failed) or nil if job can't be locked. - def run_with_lock(max_run_time, worker_name) - logger.info "* [JOB] aquiring lock on #{name}" - unless lock_exclusively!(max_run_time, worker_name) - # We did not get the lock, some other worker process must have - logger.warn "* [JOB] failed to aquire exclusive lock for #{name}" - return nil # no work done - end - - begin - runtime = Benchmark.realtime do - invoke_job # TODO: raise error if takes longer than max_run_time - destroy - end - # TODO: warn if runtime > max_run_time ? - logger.info "* [JOB] #{name} completed after %.4f" % runtime - return true # did work - rescue Exception => e - reschedule e.message, e.backtrace - log_exception(e) - return false # work failed - end - end - # Add a job to the queue - def self.enqueue(*args, &block) - object = block_given? ? EvaledJob.new(&block) : args.shift - - unless object.respond_to?(:perform) || block_given? + def self.enqueue(*args) + object = args.shift + unless object.respond_to?(:perform) raise ArgumentError, 'Cannot enqueue items which do not respond to perform' end @@ -117,55 +60,23 @@ def self.enqueue(*args, &block) end # Find a few candidate jobs to run (in case some immediately get locked by others). - # Return in random order prevent everyone trying to do same head job at once. - def self.find_available(limit = 5, max_run_time = MAX_RUN_TIME) - - time_now = db_time_now - - sql = NextTaskSQL.dup - - conditions = [time_now, time_now - max_run_time, worker_name] - - if self.min_priority - sql << ' AND (priority >= ?)' - conditions << min_priority - end - - if self.max_priority - sql << ' AND (priority <= ?)' - conditions << max_priority - end - - conditions.unshift(sql) - - records = ActiveRecord::Base.silence do - find(:all, :conditions => conditions, :order => NextTaskOrder, :limit => limit) - end - - records.sort_by { rand() } - end - - # Run the next job we can get an exclusive lock on. - # If no jobs are left we return nil - def self.reserve_and_run_one_job(max_run_time = MAX_RUN_TIME) - - # We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next. - # this leads to a more even distribution of jobs across the worker processes - find_available(5, max_run_time).each do |job| - t = job.run_with_lock(max_run_time, worker_name) - return t unless t == nil # return if we did work (good or bad) + def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time) + scope = self.ready_to_run(worker_name, max_run_time) + scope = scope.scoped(:conditions => ['priority >= ?', Worker.min_priority]) if Worker.min_priority + scope = scope.scoped(:conditions => ['priority <= ?', Worker.max_priority]) if Worker.max_priority + + ActiveRecord::Base.silence do + scope.by_priority.all(:limit => limit) end - - nil # we didn't do any work, all 5 were not lockable end # Lock this job for this worker. # Returns true if we have the lock, false otherwise. - def lock_exclusively!(max_run_time, worker = worker_name) + def lock_exclusively!(max_run_time, worker) now = self.class.db_time_now affected_rows = if locked_by != worker # We don't own this job so we will update the locked_by name and the locked_at - self.class.update_all(["locked_at = ?, locked_by = ?", now, worker], ["id = ? and (locked_at is null or locked_at < ?)", id, (now - max_run_time.to_i)]) + self.class.update_all(["locked_at = ?, locked_by = ?", now, worker], ["id = ? and (locked_at is null or locked_at < ?) and (run_at <= ?)", id, (now - max_run_time.to_i), now]) else # We already own this job, this may happen if the job queue crashes. # Simply resume and update the locked_at @@ -186,32 +97,6 @@ def unlock self.locked_by = nil end - # This is a good hook if you need to report job processing errors in additional or different ways - def log_exception(error) - logger.error "* [JOB] #{name} failed with #{error.class.name}: #{error.message} - #{attempts} failed attempts" - logger.error(error) - end - - # Do num jobs and return stats on success/failure. - # Exit early if interrupted. - def self.work_off(num = 100) - success, failure = 0, 0 - - num.times do - case self.reserve_and_run_one_job - when true - success += 1 - when false - failure += 1 - else - break # leave if no work could be done - end - break if $exit # leave if we're exiting - end - - return [success, failure] - end - # Moved into its own method so that new_relic can trace it. def invoke_job payload_object.perform @@ -233,7 +118,7 @@ def deserialize(source) return handler if handler.respond_to?(:perform) raise DeserializationError, - 'Job failed to load: Unknown handler. Try to manually require the appropiate file.' + 'Job failed to load: Unknown handler. Try to manually require the appropriate file.' rescue TypeError, LoadError, NameError => e raise DeserializationError, "Job failed to load: #{e.message}. Try to manually require the required file." @@ -249,7 +134,13 @@ def attempt_to_load(klass) # Note: This does not ping the DB to get the time, so all your clients # must have syncronized clocks. def self.db_time_now - (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now + if Time.zone + Time.zone.now + elsif ActiveRecord::Base.default_timezone == :utc + Time.now.utc + else + Time.now + end end protected @@ -259,14 +150,4 @@ def before_save end end - - class EvaledJob - def initialize - @job = yield - end - - def perform - eval(@job) - end - end end diff --git a/vendor/plugins/delayed_job/lib/delayed/message_sending.rb b/vendor/plugins/delayed_job/lib/delayed/message_sending.rb index ed75dda36f..650fb446e4 100644 --- a/vendor/plugins/delayed_job/lib/delayed/message_sending.rb +++ b/vendor/plugins/delayed_job/lib/delayed/message_sending.rb @@ -3,12 +3,17 @@ module MessageSending def send_later(method, *args) Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sym, args) end + + def send_at(time, method, *args) + Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sym, args), 0, time) + end module ClassMethods def handle_asynchronously(method) - without_name = "#{method}_without_send_later" - define_method("#{method}_with_send_later") do |*args| - send_later(without_name, *args) + aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 + with_method, without_method = "#{aliased_method}_with_send_later#{punctuation}", "#{aliased_method}_without_send_later#{punctuation}" + define_method(with_method) do |*args| + send_later(without_method, *args) end alias_method_chain method, :send_later end diff --git a/vendor/plugins/delayed_job/lib/delayed/recipes.rb b/vendor/plugins/delayed_job/lib/delayed/recipes.rb new file mode 100644 index 0000000000..c710bb6bdb --- /dev/null +++ b/vendor/plugins/delayed_job/lib/delayed/recipes.rb @@ -0,0 +1,31 @@ +# Capistrano Recipes for managing delayed_job +# +# Add these callbacks to have the delayed_job process restart when the server +# is restarted: +# +# after "deploy:stop", "delayed_job:stop" +# after "deploy:start", "delayed_job:start" +# after "deploy:restart", "delayed_job:restart" + +Capistrano::Configuration.instance.load do + namespace :delayed_job do + def rails_env + fetch(:rails_env, false) ? "RAILS_ENV=#{fetch(:rails_env)}" : '' + end + + desc "Stop the delayed_job process" + task :stop, :roles => :app do + run "cd #{current_path};#{rails_env} script/delayed_job stop" + end + + desc "Start the delayed_job process" + task :start, :roles => :app do + run "cd #{current_path};#{rails_env} script/delayed_job start" + end + + desc "Restart the delayed_job process" + task :restart, :roles => :app do + run "cd #{current_path};#{rails_env} script/delayed_job restart" + end + end +end \ No newline at end of file diff --git a/vendor/plugins/delayed_job/tasks/tasks.rb b/vendor/plugins/delayed_job/lib/delayed/tasks.rb similarity index 100% rename from vendor/plugins/delayed_job/tasks/tasks.rb rename to vendor/plugins/delayed_job/lib/delayed/tasks.rb diff --git a/vendor/plugins/delayed_job/lib/delayed/worker.rb b/vendor/plugins/delayed_job/lib/delayed/worker.rb index 744e20ac8c..3d36d1fa2b 100644 --- a/vendor/plugins/delayed_job/lib/delayed/worker.rb +++ b/vendor/plugins/delayed_job/lib/delayed/worker.rb @@ -1,22 +1,47 @@ module Delayed class Worker - SLEEP = 5 - - cattr_accessor :logger + cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time, :sleep_delay, :logger + self.sleep_delay = 5 + self.max_attempts = 25 + self.max_run_time = 4.hours + + # By default failed jobs are destroyed after too many attempts. If you want to keep them around + # (perhaps to inspect the reason for the failure), set this to false. + cattr_accessor :destroy_failed_jobs + self.destroy_failed_jobs = true + self.logger = if defined?(Merb::Logger) Merb.logger elsif defined?(RAILS_DEFAULT_LOGGER) RAILS_DEFAULT_LOGGER end + # name_prefix is ignored if name is set directly + attr_accessor :name_prefix + def initialize(options={}) @quiet = options[:quiet] - Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority) - Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority) + self.class.min_priority = options[:min_priority] if options.has_key?(:min_priority) + self.class.max_priority = options[:max_priority] if options.has_key?(:max_priority) + end + + # Every worker has a unique name which by default is the pid of the process. There are some + # advantages to overriding this with something which survives worker retarts: Workers can# + # safely resume working on tasks which are locked by themselves. The worker will assume that + # it crashed before. + def name + return @name unless @name.nil? + "#{@name_prefix}host:#{Socket.gethostname} pid:#{Process.pid}" rescue "#{@name_prefix}pid:#{Process.pid}" + end + + # Sets the name of the worker. + # Setting the name to nil will reset the default worker name + def name=(val) + @name = val end def start - say "*** Starting job worker #{Delayed::Job.worker_name}" + say "*** Starting job worker #{name}" trap('TERM') { say 'Exiting...'; $exit = true } trap('INT') { say 'Exiting...'; $exit = true } @@ -25,7 +50,7 @@ def start result = nil realtime = Benchmark.realtime do - result = Delayed::Job.work_off + result = work_off end count = result.sum @@ -33,7 +58,7 @@ def start break if $exit if count.zero? - sleep(SLEEP) + sleep(@@sleep_delay) else say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last] end @@ -42,13 +67,86 @@ def start end ensure - Delayed::Job.clear_locks! + Delayed::Job.clear_locks!(name) + end + + def run(job) + runtime = Benchmark.realtime do + Timeout.timeout(self.class.max_run_time.to_i) { job.invoke_job } + job.destroy + end + # TODO: warn if runtime > max_run_time ? + say "* [JOB] #{name} completed after %.4f" % runtime + return true # did work + rescue Exception => e + handle_failed_job(job, e) + return false # work failed + end + + # Reschedule the job in the future (when a job fails). + # Uses an exponential scale depending on the number of failed attempts. + def reschedule(job, time = nil) + if (job.attempts += 1) < self.class.max_attempts + time ||= Job.db_time_now + (job.attempts ** 4) + 5 + job.run_at = time + job.unlock + job.save! + else + say "* [JOB] PERMANENTLY removing #{job.name} because of #{job.attempts} consecutive failures.", Logger::INFO + self.class.destroy_failed_jobs ? job.destroy : job.update_attribute(:failed_at, Delayed::Job.db_time_now) + end end - def say(text) + def say(text, level = Logger::INFO) puts text unless @quiet - logger.info text if logger + logger.add level, text if logger + end + + protected + + def handle_failed_job(job, error) + job.last_error = error.message + "\n" + error.backtrace.join("\n") + say "* [JOB] #{name} failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts", Logger::ERROR + reschedule(job) end + + # Run the next job we can get an exclusive lock on. + # If no jobs are left we return nil + def reserve_and_run_one_job + # We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next. + # this leads to a more even distribution of jobs across the worker processes + job = Delayed::Job.find_available(name, 5, self.class.max_run_time).detect do |job| + if job.lock_exclusively!(self.class.max_run_time, name) + say "* [Worker(#{name})] acquired lock on #{job.name}" + true + else + say "* [Worker(#{name})] failed to acquire exclusive lock for #{job.name}", Logger::WARN + false + end + end + + run(job) if job + end + + # Do num jobs and return stats on success/failure. + # Exit early if interrupted. + def work_off(num = 100) + success, failure = 0, 0 + + num.times do + case reserve_and_run_one_job + when true + success += 1 + when false + failure += 1 + else + break # leave if no work could be done + end + break if $exit # leave if we're exiting + end + + return [success, failure] + end end end diff --git a/vendor/plugins/delayed_job/lib/delayed_job.rb b/vendor/plugins/delayed_job/lib/delayed_job.rb index 482cb2ff05..d7d0788157 100644 --- a/vendor/plugins/delayed_job/lib/delayed_job.rb +++ b/vendor/plugins/delayed_job/lib/delayed_job.rb @@ -9,5 +9,5 @@ Module.send(:include, Delayed::MessageSending::ClassMethods) if defined?(Merb::Plugins) - Merb::Plugins.add_rakefiles File.dirname(__FILE__) / '..' / 'tasks' / 'tasks' + Merb::Plugins.add_rakefiles File.dirname(__FILE__) / 'delayed' / 'tasks' end diff --git a/vendor/plugins/delayed_job/recipes/delayed_job.rb b/vendor/plugins/delayed_job/recipes/delayed_job.rb new file mode 100644 index 0000000000..05d4baa4f6 --- /dev/null +++ b/vendor/plugins/delayed_job/recipes/delayed_job.rb @@ -0,0 +1 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'delayed', 'recipes')) diff --git a/vendor/plugins/delayed_job/spec/delayed_method_spec.rb b/vendor/plugins/delayed_job/spec/delayed_method_spec.rb index e3911dcdcb..e9e987247a 100644 --- a/vendor/plugins/delayed_job/spec/delayed_method_spec.rb +++ b/vendor/plugins/delayed_job/spec/delayed_method_spec.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/database' +require 'spec_helper' class SimpleJob cattr_accessor :runs; self.runs = 0 @@ -73,15 +73,8 @@ def read(story) end it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do - - ErrorObject.new.send_later(:throw) - - Delayed::Job.count.should == 1 - - Delayed::Job.reserve_and_run_one_job - - Delayed::Job.count.should == 0 - + job = ErrorObject.new.send_later(:throw) + lambda { job.invoke_job }.should_not raise_error end it "should store the object as string if its an active record" do @@ -125,4 +118,26 @@ def read(story) job.payload_object.perform.should == 'Once upon...' end + context "send_at" do + it "should queue a new job" do + lambda do + "string".send_at(1.hour.from_now, :length) + end.should change { Delayed::Job.count }.by(1) + end + + it "should schedule the job in the future" do + time = 1.hour.from_now + job = "string".send_at(time, :length) + job.run_at.should == time + end + + it "should store payload as PerformableMethod" do + job = "string".send_at(1.hour.from_now, :count, 'r') + job.payload_object.class.should == Delayed::PerformableMethod + job.payload_object.method.should == :count + job.payload_object.args.should == ['r'] + job.payload_object.perform.should == 1 + end + end + end diff --git a/vendor/plugins/delayed_job/spec/job_spec.rb b/vendor/plugins/delayed_job/spec/job_spec.rb index 9d3bc303ff..a8879cfc69 100644 --- a/vendor/plugins/delayed_job/spec/job_spec.rb +++ b/vendor/plugins/delayed_job/spec/job_spec.rb @@ -1,27 +1,9 @@ -require File.dirname(__FILE__) + '/database' - -class SimpleJob - cattr_accessor :runs; self.runs = 0 - def perform; @@runs += 1; end -end - -class ErrorJob - cattr_accessor :runs; self.runs = 0 - def perform; raise 'did not work'; end -end - -module M - class ModuleJob - cattr_accessor :runs; self.runs = 0 - def perform; @@runs += 1; end - end - -end +require 'spec_helper' describe Delayed::Job do before do - Delayed::Job.max_priority = nil - Delayed::Job.min_priority = nil + Delayed::Worker.max_priority = nil + Delayed::Worker.min_priority = nil Delayed::Job.delete_all end @@ -61,52 +43,11 @@ def perform; @@runs += 1; end Delayed::Job.first.run_at.should be_close(later, 1) end - it "should call perform on jobs when running work_off" do - SimpleJob.runs.should == 0 - - Delayed::Job.enqueue SimpleJob.new - Delayed::Job.work_off - - SimpleJob.runs.should == 1 - end - - - it "should work with eval jobs" do - $eval_job_ran = false - - Delayed::Job.enqueue do <<-JOB - $eval_job_ran = true - JOB - end - - Delayed::Job.work_off - - $eval_job_ran.should == true - end - it "should work with jobs in modules" do - M::ModuleJob.runs.should == 0 - - Delayed::Job.enqueue M::ModuleJob.new - Delayed::Job.work_off - - M::ModuleJob.runs.should == 1 + job = Delayed::Job.enqueue M::ModuleJob.new + lambda { job.invoke_job }.should change { M::ModuleJob.runs }.from(0).to(1) end - it "should re-schedule by about 1 second at first and increment this more and more minutes when it fails to execute properly" do - Delayed::Job.enqueue ErrorJob.new - Delayed::Job.work_off(1) - - job = Delayed::Job.find(:first) - - job.last_error.should =~ /did not work/ - job.last_error.should =~ /job_spec.rb:10:in `perform'/ - job.attempts.should == 1 - - job.run_at.should > Delayed::Job.db_time_now - 10.minutes - job.run_at.should < Delayed::Job.db_time_now + 10.minutes - end - it "should raise an DeserializationError when the job class is totally unknown" do job = Delayed::Job.new @@ -148,38 +89,14 @@ def perform; @@runs += 1; end lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError) end - it "should be failed if it failed more than MAX_ATTEMPTS times and we don't want to destroy jobs" do - default = Delayed::Job.destroy_failed_jobs - Delayed::Job.destroy_failed_jobs = false - - @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50 - @job.reload.failed_at.should == nil - @job.reschedule 'FAIL' - @job.reload.failed_at.should_not == nil - - Delayed::Job.destroy_failed_jobs = default - end - - it "should be destroyed if it failed more than MAX_ATTEMPTS times and we want to destroy jobs" do - default = Delayed::Job.destroy_failed_jobs - Delayed::Job.destroy_failed_jobs = true - - @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50 - @job.should_receive(:destroy) - @job.reschedule 'FAIL' - - Delayed::Job.destroy_failed_jobs = default - end - it "should never find failed jobs" do @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50, :failed_at => Delayed::Job.db_time_now - Delayed::Job.find_available(1).length.should == 0 + Delayed::Job.find_available('worker', 1).length.should == 0 end context "when another worker is already performing an task, it" do before :each do - Delayed::Job.worker_name = 'worker1' @job = Delayed::Job.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => Delayed::Job.db_time_now - 5.minutes end @@ -202,24 +119,38 @@ def perform; @@runs += 1; end end it "should not be found by another worker" do - Delayed::Job.worker_name = 'worker2' - - Delayed::Job.find_available(1, 6.minutes).length.should == 0 + Delayed::Job.find_available('worker2', 1, 6.minutes).length.should == 0 end it "should be found by another worker if the time has expired" do - Delayed::Job.worker_name = 'worker2' - - Delayed::Job.find_available(1, 4.minutes).length.should == 1 + Delayed::Job.find_available('worker2', 1, 4.minutes).length.should == 1 end it "should be able to get exclusive access again when the worker name is the same" do - @job.lock_exclusively! 5.minutes, 'worker1' - @job.lock_exclusively! 5.minutes, 'worker1' - @job.lock_exclusively! 5.minutes, 'worker1' + @job.lock_exclusively!(5.minutes, 'worker1').should be_true + @job.lock_exclusively!(5.minutes, 'worker1').should be_true + @job.lock_exclusively!(5.minutes, 'worker1').should be_true end - end + end + context "when another worker has worked on a task since the job was found to be available, it" do + + before :each do + @job = Delayed::Job.create :payload_object => SimpleJob.new + @job_copy_for_worker_2 = Delayed::Job.find(@job.id) + end + + it "should not allow a second worker to get exclusive access if already successfully processed by worker1" do + @job.delete + @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false + end + + it "should not allow a second worker to get exclusive access if failed to be processed by worker1 and run_at time is now in future (due to backing off behaviour)" do + @job.update_attributes(:attempts => 1, :run_at => 1.day.from_now) + @job_copy_for_worker_2.lock_exclusively!(4.hours, 'worker2').should == false + end + end + context "#name" do it "should be the class name of the job that was enqueued" do Delayed::Job.create(:payload_object => ErrorJob.new ).name.should == 'ErrorJob' @@ -242,104 +173,43 @@ def perform; @@runs += 1; end context "worker prioritization" do before(:each) do - Delayed::Job.max_priority = nil - Delayed::Job.min_priority = nil - end - - it "should only work_off jobs that are >= min_priority" do - Delayed::Job.min_priority = -5 - Delayed::Job.max_priority = 5 - SimpleJob.runs.should == 0 - - Delayed::Job.enqueue SimpleJob.new, -10 - Delayed::Job.enqueue SimpleJob.new, 0 - Delayed::Job.work_off - - SimpleJob.runs.should == 1 + Delayed::Worker.max_priority = nil + Delayed::Worker.min_priority = nil + end + + it "should fetch jobs ordered by priority" do + number_of_jobs = 10 + number_of_jobs.times { Delayed::Job.enqueue SimpleJob.new, rand(10) } + jobs = Delayed::Job.find_available('worker', 10) + ordered = true + jobs[1..-1].each_index{ |i| + if (jobs[i].priority < jobs[i+1].priority) + ordered = false + break + end + } + ordered.should == true end - - it "should only work_off jobs that are <= max_priority" do - Delayed::Job.min_priority = -5 - Delayed::Job.max_priority = 5 - SimpleJob.runs.should == 0 - - Delayed::Job.enqueue SimpleJob.new, 10 - Delayed::Job.enqueue SimpleJob.new, 0 - - Delayed::Job.work_off - - SimpleJob.runs.should == 1 - end end - context "when pulling jobs off the queue for processing, it" do - before(:each) do - @job = Delayed::Job.create( - :payload_object => SimpleJob.new, - :locked_by => 'worker1', - :locked_at => Delayed::Job.db_time_now - 5.minutes) + context "db_time_now" do + it "should return time in current time zone if set" do + Time.zone = 'Eastern Time (US & Canada)' + Delayed::Job.db_time_now.zone.should == 'EST' end - - it "should leave the queue in a consistent state and not run the job if locking fails" do - SimpleJob.runs.should == 0 - @job.stub!(:lock_exclusively!).with(any_args).once.and_return(false) - Delayed::Job.should_receive(:find_available).once.and_return([@job]) - Delayed::Job.work_off(1) - SimpleJob.runs.should == 0 - end - - end - - context "while running alongside other workers that locked jobs, it" do - before(:each) do - Delayed::Job.worker_name = 'worker1' - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) - Delayed::Job.create(:payload_object => SimpleJob.new) - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) - end - - it "should ingore locked jobs from other workers" do - Delayed::Job.worker_name = 'worker3' - SimpleJob.runs.should == 0 - Delayed::Job.work_off - SimpleJob.runs.should == 1 # runs the one open job - end - - it "should find our own jobs regardless of locks" do - Delayed::Job.worker_name = 'worker1' - SimpleJob.runs.should == 0 - Delayed::Job.work_off - SimpleJob.runs.should == 3 # runs open job plus worker1 jobs that were already locked - end - end - - context "while running with locked and expired jobs, it" do - before(:each) do - Delayed::Job.worker_name = 'worker1' - exp_time = Delayed::Job.db_time_now - (1.minutes + Delayed::Job::MAX_RUN_TIME) - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => exp_time) - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker2', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) - Delayed::Job.create(:payload_object => SimpleJob.new) - Delayed::Job.create(:payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) - end - - it "should only find unlocked and expired jobs" do - Delayed::Job.worker_name = 'worker3' - SimpleJob.runs.should == 0 - Delayed::Job.work_off - SimpleJob.runs.should == 2 # runs the one open job and one expired job + + it "should return UTC time if that is the AR default" do + Time.zone = nil + ActiveRecord::Base.default_timezone = :utc + Delayed::Job.db_time_now.zone.should == 'UTC' end - it "should ignore locks when finding our own jobs" do - Delayed::Job.worker_name = 'worker1' - SimpleJob.runs.should == 0 - Delayed::Job.work_off - SimpleJob.runs.should == 3 # runs open job plus worker1 jobs - # This is useful in the case of a crash/restart on worker1, but make sure multiple workers on the same host have unique names! + it "should return local time if that is the AR default" do + Time.zone = nil + ActiveRecord::Base.default_timezone = :local + Delayed::Job.db_time_now.zone.should_not == 'UTC' end - end end diff --git a/vendor/plugins/delayed_job/spec/sample_jobs.rb b/vendor/plugins/delayed_job/spec/sample_jobs.rb new file mode 100644 index 0000000000..36e3c9252c --- /dev/null +++ b/vendor/plugins/delayed_job/spec/sample_jobs.rb @@ -0,0 +1,21 @@ +class SimpleJob + cattr_accessor :runs; self.runs = 0 + def perform; @@runs += 1; end +end + +class ErrorJob + cattr_accessor :runs; self.runs = 0 + def perform; raise 'did not work'; end +end + +class LongRunningJob + def perform; sleep 250; end +end + +module M + class ModuleJob + cattr_accessor :runs; self.runs = 0 + def perform; @@runs += 1; end + end + +end \ No newline at end of file diff --git a/vendor/plugins/delayed_job/spec/database.rb b/vendor/plugins/delayed_job/spec/spec_helper.rb similarity index 80% rename from vendor/plugins/delayed_job/spec/database.rb rename to vendor/plugins/delayed_job/spec/spec_helper.rb index d25578d42f..1af64929e7 100644 --- a/vendor/plugins/delayed_job/spec/database.rb +++ b/vendor/plugins/delayed_job/spec/spec_helper.rb @@ -1,17 +1,13 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') -$:.unshift(File.dirname(__FILE__) + '/../../rspec/lib') require 'rubygems' -require 'active_record' -gem 'sqlite3-ruby' - -require File.dirname(__FILE__) + '/../init' require 'spec' +require 'active_record' +require 'delayed_job' ActiveRecord::Base.logger = Logger.new('/tmp/dj.log') -ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/jobs.sqlite') +ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') ActiveRecord::Migration.verbose = false -ActiveRecord::Base.default_timezone = :utc if Time.zone.nil? ActiveRecord::Schema.define do @@ -33,7 +29,6 @@ end - # Purely useful for test cases... class Story < ActiveRecord::Base def tell; text; end @@ -41,3 +36,5 @@ def whatever(n, _); tell*n; end handle_asynchronously :whatever end + +require 'sample_jobs' diff --git a/vendor/plugins/delayed_job/spec/story_spec.rb b/vendor/plugins/delayed_job/spec/story_spec.rb index 0144b44d40..61d5f3a611 100644 --- a/vendor/plugins/delayed_job/spec/story_spec.rb +++ b/vendor/plugins/delayed_job/spec/story_spec.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/database' +require 'spec_helper' describe "A story" do diff --git a/vendor/plugins/delayed_job/spec/worker_spec.rb b/vendor/plugins/delayed_job/spec/worker_spec.rb new file mode 100644 index 0000000000..2cc5e6ff62 --- /dev/null +++ b/vendor/plugins/delayed_job/spec/worker_spec.rb @@ -0,0 +1,161 @@ +require 'spec_helper' + +describe Delayed::Worker do + def job_create(opts = {}) + Delayed::Job.create(opts.merge(:payload_object => SimpleJob.new)) + end + + before(:all) do + Delayed::Worker.send :public, :work_off + end + + before(:each) do + @worker = Delayed::Worker.new(:max_priority => nil, :min_priority => nil, :quiet => true) + + Delayed::Job.delete_all + + SimpleJob.runs = 0 + end + + describe "running a job" do + it "should fail after Worker.max_run_time" do + begin + old_max_run_time = Delayed::Worker.max_run_time + Delayed::Worker.max_run_time = 1.second + @job = Delayed::Job.create :payload_object => LongRunningJob.new + @worker.run(@job) + @job.reload.last_error.should =~ /expired/ + @job.attempts.should == 1 + ensure + Delayed::Worker.max_run_time = old_max_run_time + end + end + end + + context "worker prioritization" do + before(:each) do + @worker = Delayed::Worker.new(:max_priority => 5, :min_priority => -5, :quiet => true) + end + + it "should only work_off jobs that are >= min_priority" do + SimpleJob.runs.should == 0 + + job_create(:priority => -10) + job_create(:priority => 0) + @worker.work_off + + SimpleJob.runs.should == 1 + end + + it "should only work_off jobs that are <= max_priority" do + SimpleJob.runs.should == 0 + + job_create(:priority => 10) + job_create(:priority => 0) + + @worker.work_off + + SimpleJob.runs.should == 1 + end + end + + context "while running with locked and expired jobs" do + before(:each) do + @worker.name = 'worker1' + end + + it "should not run jobs locked by another worker" do + job_create(:locked_by => 'other_worker', :locked_at => (Delayed::Job.db_time_now - 1.minutes)) + lambda { @worker.work_off }.should_not change { SimpleJob.runs } + end + + it "should run open jobs" do + job_create + lambda { @worker.work_off }.should change { SimpleJob.runs }.from(0).to(1) + end + + it "should run expired jobs" do + expired_time = Delayed::Job.db_time_now - (1.minutes + Delayed::Worker.max_run_time) + job_create(:locked_by => 'other_worker', :locked_at => expired_time) + lambda { @worker.work_off }.should change { SimpleJob.runs }.from(0).to(1) + end + + it "should run own jobs" do + job_create(:locked_by => @worker.name, :locked_at => (Delayed::Job.db_time_now - 1.minutes)) + lambda { @worker.work_off }.should change { SimpleJob.runs }.from(0).to(1) + end + end + + describe "failed jobs" do + before do + # reset defaults + Delayed::Worker.destroy_failed_jobs = true + Delayed::Worker.max_attempts = 25 + + @job = Delayed::Job.enqueue ErrorJob.new + end + + it "should record last_error when destroy_failed_jobs = false, max_attempts = 1" do + Delayed::Worker.destroy_failed_jobs = false + Delayed::Worker.max_attempts = 1 + @worker.run(@job) + @job.reload + @job.last_error.should =~ /did not work/ + @job.last_error.should =~ /worker_spec.rb/ + @job.attempts.should == 1 + @job.failed_at.should_not be_nil + end + + it "should re-schedule jobs after failing" do + @worker.run(@job) + @job.reload + @job.last_error.should =~ /did not work/ + @job.last_error.should =~ /sample_jobs.rb:8:in `perform'/ + @job.attempts.should == 1 + @job.run_at.should > Delayed::Job.db_time_now - 10.minutes + @job.run_at.should < Delayed::Job.db_time_now + 10.minutes + end + end + + context "reschedule" do + before do + @job = Delayed::Job.create :payload_object => SimpleJob.new + end + + context "and we want to destroy jobs" do + before do + Delayed::Worker.destroy_failed_jobs = true + end + + it "should be destroyed if it failed more than Worker.max_attempts times" do + @job.should_receive(:destroy) + Delayed::Worker.max_attempts.times { @worker.reschedule(@job) } + end + + it "should not be destroyed if failed fewer than Worker.max_attempts times" do + @job.should_not_receive(:destroy) + (Delayed::Worker.max_attempts - 1).times { @worker.reschedule(@job) } + end + end + + context "and we don't want to destroy jobs" do + before do + Delayed::Worker.destroy_failed_jobs = false + end + + it "should be failed if it failed more than Worker.max_attempts times" do + @job.reload.failed_at.should == nil + Delayed::Worker.max_attempts.times { @worker.reschedule(@job) } + @job.reload.failed_at.should_not == nil + end + + it "should not be failed if it failed fewer than Worker.max_attempts times" do + (Delayed::Worker.max_attempts - 1).times { @worker.reschedule(@job) } + @job.reload.failed_at.should == nil + end + + end + end + + +end diff --git a/vendor/plugins/delayed_job/tasks/jobs.rake b/vendor/plugins/delayed_job/tasks/jobs.rake index d044517112..f3993f75eb 100644 --- a/vendor/plugins/delayed_job/tasks/jobs.rake +++ b/vendor/plugins/delayed_job/tasks/jobs.rake @@ -1 +1 @@ -require File.join(File.dirname(__FILE__), 'tasks') +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'delayed', 'tasks'))