delayed_job_mongoid 1.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +10 -0
- data/lib/delayed/backend/mongoid.rb +82 -0
- data/lib/delayed/serialization/mongoid.rb +16 -0
- data/lib/delayed_job_mongoid.rb +6 -0
- data/spec/delayed_job_mongoid_spec.rb +29 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +26 -0
- metadata +114 -0
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,82 @@
|
|
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, :index => true
|
14
|
+
field :failed_at, :type=> Time
|
15
|
+
field :last_error, :type=> String
|
16
|
+
|
17
|
+
|
18
|
+
before_save :set_default_run_at
|
19
|
+
|
20
|
+
def self.before_fork
|
21
|
+
::Mongoid.master.connection.close
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.after_fork
|
25
|
+
::Mongoid.master.connection.connect_to_master
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.db_time_now
|
29
|
+
Time.now.utc
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
|
33
|
+
right_now = db_time_now
|
34
|
+
|
35
|
+
conditions = {:run_at => {"$lte" => right_now}, :failed_at => nil}
|
36
|
+
(conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
|
37
|
+
(conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
|
38
|
+
|
39
|
+
|
40
|
+
where = "this.locked_at == null || this.locked_at < #{make_date(right_now - max_run_time)}"
|
41
|
+
results = self.where(conditions.merge(:locked_by => worker_name)).limit(-limit).order_by([['priority', 1], ['run_at', 1]]).to_a
|
42
|
+
results += self.where(conditions.merge('$where' => where)).limit(-limit+results.size).order_by([['priority', 1], ['run_at', 1]]).to_a if results.size < limit
|
43
|
+
results
|
44
|
+
end
|
45
|
+
# When a worker is exiting, make sure we don't have any locked jobs.
|
46
|
+
def self.clear_locks!(worker_name)
|
47
|
+
self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Lock this job for this worker.
|
51
|
+
# Returns true if we have the lock, false otherwise.
|
52
|
+
def lock_exclusively!(max_run_time, worker = worker_name)
|
53
|
+
right_now = self.class.db_time_now
|
54
|
+
overtime = right_now - max_run_time.to_i
|
55
|
+
|
56
|
+
query = "this.locked_at == null || this.locked_at < #{make_date(overtime)} || this.locked_by == #{worker.to_json}"
|
57
|
+
conditions = {:_id => id, :run_at => {"$lte" => right_now}, "$where" => query}
|
58
|
+
|
59
|
+
self.collection.update(conditions, {"$set" => {:locked_at => right_now, :locked_by => worker}})
|
60
|
+
affected_rows = self.collection.find({:_id => id, :locked_by => worker}).count
|
61
|
+
if affected_rows == 1
|
62
|
+
self.locked_at = right_now
|
63
|
+
self.locked_by = worker
|
64
|
+
return true
|
65
|
+
else
|
66
|
+
return false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def self.make_date(date_or_seconds)
|
73
|
+
"new Date(#{date_or_seconds.to_f * 1000})"
|
74
|
+
end
|
75
|
+
|
76
|
+
def make_date(date)
|
77
|
+
self.class.make_date(date)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
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
|
+
nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_yaml_properties
|
13
|
+
['@attributes']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -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
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
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 'spec'
|
6
|
+
require 'spec/autorun'
|
7
|
+
require 'delayed_job_mongoid'
|
8
|
+
require 'delayed/backend/shared_spec'
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
Mongoid.configure do |config|
|
15
|
+
config.master = config.master = Mongo::Connection.new.db('dl_spec')
|
16
|
+
end
|
17
|
+
|
18
|
+
class Story
|
19
|
+
include ::Mongoid::Document
|
20
|
+
def tell; text; end
|
21
|
+
def whatever(n, _); tell*n; end
|
22
|
+
def self.count; end
|
23
|
+
|
24
|
+
handle_asynchronously :whatever
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delayed_job_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- rc
|
10
|
+
version: 1.0.0.rc
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brandon Keepers
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-05 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongoid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
version: "2.0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: delayed_job
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 1
|
44
|
+
version: "2.1"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 1
|
56
|
+
- 2
|
57
|
+
- 9
|
58
|
+
version: 1.2.9
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
description:
|
62
|
+
email: brandon@collectiveidea.com
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files:
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
files:
|
71
|
+
- lib/delayed/backend/mongoid.rb
|
72
|
+
- lib/delayed/serialization/mongoid.rb
|
73
|
+
- lib/delayed_job_mongoid.rb
|
74
|
+
- spec/delayed_job_mongoid_spec.rb
|
75
|
+
- spec/spec.opts
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
has_rdoc: true
|
80
|
+
homepage: http://github.com/collectiveidea/delayed_job_mongoid
|
81
|
+
licenses: []
|
82
|
+
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options:
|
85
|
+
- --charset=UTF-8
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 1
|
101
|
+
- 3
|
102
|
+
- 1
|
103
|
+
version: 1.3.1
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.3.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Mongoid backend for delayed_job
|
111
|
+
test_files:
|
112
|
+
- spec/delayed_job_mongoid_spec.rb
|
113
|
+
- spec/spec.opts
|
114
|
+
- spec/spec_helper.rb
|