delayed_job_mongoid_beta_compatible 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright © 2010 Collective Idea
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # delayed_job Mongoid backend
2
+
3
+ ## Installation
4
+
5
+ Add the gems to your Gemfile:
6
+
7
+ gem 'delayed_job'
8
+ gem 'delayed_job_mongoid'
9
+
10
+ After running `bundle install`, create the indexes (and don't forget to do this on your production database):
11
+
12
+ script/rails runner 'Delayed::Backend::Mongoid::Job.create_indexes'
13
+
14
+ That's it. Use [delayed_job as normal](http://github.com/collectiveidea/delayed_job).
@@ -0,0 +1,79 @@
1
+ module Delayed
2
+ module Backend
3
+ module Mongoid
4
+ class Job
5
+ include ::Mongoid::Document
6
+ include ::Mongoid::Timestamps
7
+ include Delayed::Backend::Base
8
+ field :priority, :type=> Integer, :default => 0
9
+ field :attempts, :type=> Integer, :default => 0
10
+ field :handler, :type=> String
11
+ field :run_at, :type=> Time
12
+ field :locked_at, :type=> Time
13
+ field :locked_by, :type=> String
14
+ field :failed_at, :type=> Time
15
+ field :last_error, :type=> String
16
+
17
+ index ([[:locked_by, -1], [:priority, 1], [:run_at, 1]])
18
+
19
+ before_save :set_default_run_at
20
+
21
+ def self.before_fork
22
+ ::Mongoid.master.connection.close
23
+ end
24
+
25
+ def self.after_fork
26
+ ::Mongoid.master.connection.connect
27
+ end
28
+
29
+ def self.db_time_now
30
+ Time.now.utc
31
+ end
32
+
33
+ # Reserves this job for the worker.
34
+ #
35
+ # Uses Mongo's findAndModify operation to atomically pick and lock one
36
+ # job from from the collection. findAndModify is not yet available
37
+ # directly thru Mongoid so go down to the Mongo Ruby driver instead.
38
+ def self.reserve(worker, max_run_time = Worker.max_run_time)
39
+ right_now = db_time_now
40
+
41
+ conditions = {:run_at => {"$lte" => right_now}, :failed_at => nil}
42
+ (conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
43
+ (conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
44
+
45
+ if ::Mongoid.master.connection.server_version < '1.5.3'
46
+ lock_time = "new Date(#{(right_now - max_run_time).to_f * 1000})"
47
+ conditions['$where'] = "this.locked_by == '#{worker.name}' || this.locked_at == null || this.locked_at < #{lock_time}"
48
+ else
49
+ conditions['$or'] = [
50
+ { :locked_by => worker.name },
51
+ { :locked_at => nil },
52
+ { :locked_at => { '$lt' => (right_now - max_run_time) }}
53
+ ]
54
+ end
55
+
56
+ begin
57
+ result = self.db.collection(self.collection.name).find_and_modify(
58
+ :query => conditions,
59
+ :sort => [['locked_by', -1], ['priority', 1], ['run_at', 1]],
60
+ :update => {"$set" => {:locked_at => right_now, :locked_by => worker.name}}
61
+ )
62
+
63
+ # Return result as a Mongoid document.
64
+ # When Mongoid starts supporting findAndModify, this extra step should no longer be necessary.
65
+ self.find(:first, :conditions => {:_id => result["_id"]})
66
+ rescue Mongo::OperationFailure
67
+ nil # no jobs available
68
+ end
69
+ end
70
+
71
+ # When a worker is exiting, make sure we don't have any locked jobs.
72
+ def self.clear_locks!(worker_name)
73
+ self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
74
+ end
75
+
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ Mongoid::Document.class_eval do
2
+ yaml_as "tag:ruby.yaml.org,2002:Mongoid"
3
+
4
+ def self.yaml_new(klass, tag, val)
5
+ begin
6
+ klass.find(val['attributes']['_id'])
7
+ rescue Mongoid::Errors::DocumentNotFound
8
+ raise Delayed::DeserializationError
9
+ end
10
+ end
11
+
12
+ def to_yaml_properties
13
+ ['@attributes']
14
+ end
15
+ end
16
+
@@ -0,0 +1,6 @@
1
+ require 'mongoid'
2
+ require 'delayed_job'
3
+ require 'delayed/serialization/mongoid'
4
+ require 'delayed/backend/mongoid'
5
+
6
+ Delayed::Worker.backend = :mongoid
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Delayed::Backend::Mongoid::Job do
4
+ it_should_behave_like 'a delayed_job backend'
5
+
6
+ describe "before_fork" do
7
+ after do
8
+ ::Mongoid.master.connection.close
9
+ end
10
+
11
+ it "should disconnect" do
12
+ lambda do
13
+ Delayed::Backend::Mongoid::Job.before_fork
14
+ end.should change { !!Mongoid.master.connection.connected? }.from(true).to(false)
15
+ end
16
+ end
17
+
18
+ describe "after_fork" do
19
+ before do
20
+ ::Mongoid.master.connection.close
21
+ end
22
+
23
+ it "should call reconnect" do
24
+ lambda do
25
+ Delayed::Backend::Mongoid::Job.after_fork
26
+ end.should change { !!Mongoid.master.connection.connected? }.from(false).to(true)
27
+ end
28
+ end
29
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'rspec'
6
+ require 'delayed_job_mongoid'
7
+ require 'delayed/backend/shared_spec'
8
+
9
+ Mongoid.configure do |config|
10
+ config.master = config.master = Mongo::Connection.new.db('dl_spec')
11
+ end
12
+
13
+ class Story
14
+ include ::Mongoid::Document
15
+ def tell; text; end
16
+ def whatever(n, _); tell*n; end
17
+ def self.count; end
18
+
19
+ handle_asynchronously :whatever
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_mongoid_beta_compatible
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 2
10
+ version: 1.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Chris Gaffney
14
+ - Brandon Keepers
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-08 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: mongoid
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 62196449
31
+ segments:
32
+ - 2
33
+ - 0
34
+ - 0
35
+ - beta
36
+ - 1
37
+ version: 2.0.0.beta.1
38
+ type: :runtime
39
+ version_requirements: *id001
40
+ - !ruby/object:Gem::Dependency
41
+ name: delayed_job
42
+ prerelease: false
43
+ requirement: &id002 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ hash: 9
49
+ segments:
50
+ - 2
51
+ - 1
52
+ - 1
53
+ version: 2.1.1
54
+ type: :runtime
55
+ version_requirements: *id002
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ prerelease: false
59
+ requirement: &id003 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 2
67
+ - 0
68
+ version: "2.0"
69
+ type: :development
70
+ version_requirements: *id003
71
+ description:
72
+ email:
73
+ - chris@collectiveidea.com
74
+ - brandon@opensoul.com
75
+ executables: []
76
+
77
+ extensions: []
78
+
79
+ extra_rdoc_files:
80
+ - LICENSE
81
+ - README.md
82
+ files:
83
+ - lib/delayed/backend/mongoid.rb
84
+ - lib/delayed/serialization/mongoid.rb
85
+ - lib/delayed_job_mongoid.rb
86
+ - spec/delayed_job_mongoid_spec.rb
87
+ - spec/spec.opts
88
+ - spec/spec_helper.rb
89
+ - LICENSE
90
+ - README.md
91
+ has_rdoc: true
92
+ homepage: http://github.com/collectiveidea/delayed_job_mongoid
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --charset=UTF-8
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.5.0
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Mongoid backend for delayed_job
125
+ test_files:
126
+ - spec/delayed_job_mongoid_spec.rb
127
+ - spec/spec.opts
128
+ - spec/spec_helper.rb