leifcr-delayed_job_mongo_mapper 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +14 -0
- data/lib/delayed/backend/mongo_mapper.rb +89 -0
- data/lib/delayed/serialization/mongo_mapper.rb +14 -0
- data/lib/delayed_job_mongo_mapper.rb +6 -0
- data/spec/delayed_job_mongo_mapper_spec.rb +29 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +21 -0
- metadata +112 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2010 Andrew Timberlake
|
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 MongoMapper backend
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add the gems to your Gemfile:
|
6
|
+
|
7
|
+
gem 'delayed_job'
|
8
|
+
gem 'delayed_job_mongo_mapper'
|
9
|
+
|
10
|
+
Create the indexes:
|
11
|
+
|
12
|
+
script/rails runner 'Delayed::Backend::MongoMapper::Job.create_indexes'
|
13
|
+
|
14
|
+
That's it. Use [delayed_job as normal](http://github.com/collectiveidea/delayed_job).
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Delayed
|
2
|
+
module Backend
|
3
|
+
module MongoMapper
|
4
|
+
|
5
|
+
## Persisted Job Object
|
6
|
+
## Contains the work object as YAML
|
7
|
+
class Job
|
8
|
+
include ::MongoMapper::Document
|
9
|
+
|
10
|
+
include Delayed::Backend::Base
|
11
|
+
set_collection_name 'delayed_jobs'
|
12
|
+
|
13
|
+
key :priority, Integer, :default => 0
|
14
|
+
key :attempts, Integer, :default => 0
|
15
|
+
key :handler, String
|
16
|
+
key :run_at, Time
|
17
|
+
key :locked_at, Time
|
18
|
+
key :locked_by, String #, :index => true
|
19
|
+
key :failed_at, Time
|
20
|
+
key :last_error, String
|
21
|
+
key :queue, String
|
22
|
+
|
23
|
+
timestamps!
|
24
|
+
|
25
|
+
#ensure_index [[:locked_by, -1], [:priority, 1], [:run_at, 1]]
|
26
|
+
|
27
|
+
before_save :set_default_run_at
|
28
|
+
|
29
|
+
def self.before_fork
|
30
|
+
::MongoMapper.connection.close
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.after_fork
|
34
|
+
::MongoMapper.connection.connect
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.db_time_now
|
38
|
+
Time.now.utc
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
# Reserves this job for the worker.
|
44
|
+
#
|
45
|
+
# Uses Mongo's findAndModify operation to atomically pick and lock one
|
46
|
+
# job from from the collection. findAndModify is not yet available
|
47
|
+
# directly thru MongoMapper so go down to the Mongo Ruby driver instead.
|
48
|
+
def self.reserve(worker, max_run_time = Worker.max_run_time)
|
49
|
+
right_now = db_time_now
|
50
|
+
|
51
|
+
|
52
|
+
conditions = {:run_at => {"$lte" => right_now}, :failed_at => nil}
|
53
|
+
(conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
|
54
|
+
(conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
|
55
|
+
conditions[:queue] = { "$in" => Worker.queues } if Worker.queues.any?
|
56
|
+
|
57
|
+
|
58
|
+
conditions['$or'] = [
|
59
|
+
{ :locked_by => worker.name },
|
60
|
+
{ :locked_at => nil },
|
61
|
+
{ :locked_at => { '$lt' => (right_now - max_run_time) }}
|
62
|
+
]
|
63
|
+
|
64
|
+
begin
|
65
|
+
result = self.collection.find_and_modify(
|
66
|
+
:query => conditions,
|
67
|
+
:sort => [['locked_by', -1], ['priority', -1], ['run_at', 1]],
|
68
|
+
:update => {"$set" => {:locked_at => right_now, :locked_by => worker.name}}
|
69
|
+
)
|
70
|
+
|
71
|
+
# Return result as a MongoMapper document.
|
72
|
+
# When MongoMapper starts supporting findAndModify, this extra step should no longer be necessary.
|
73
|
+
self.first :conditions => {:_id => result["_id"]} unless result.nil?
|
74
|
+
rescue Mongo::OperationFailure
|
75
|
+
nil # no jobs available
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# When a worker is exiting, make sure we don't have any locked jobs.
|
80
|
+
def self.clear_locks!(worker_name)
|
81
|
+
self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
MongoMapper::Document.class_eval do
|
2
|
+
yaml_as "tag:ruby.yaml.org,2002:MongoMapper"
|
3
|
+
|
4
|
+
def self.yaml_new(klass, tag, val)
|
5
|
+
klass.find!(val['_id'])
|
6
|
+
rescue MongoMapper::DocumentNotFound
|
7
|
+
raise Delayed::DeserializationError
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_yaml_properties
|
11
|
+
['@_id']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Delayed::Backend::MongoMapper::Job do
|
4
|
+
it_should_behave_like 'a delayed_job backend'
|
5
|
+
|
6
|
+
describe "before_fork" do
|
7
|
+
after do
|
8
|
+
::MongoMapper.connection.close
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should disconnect" do
|
12
|
+
lambda do
|
13
|
+
Delayed::Backend::MongoMapper::Job.before_fork
|
14
|
+
end.should change { !!MongoMapper.connection.connected? }.from(true).to(false)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "after_fork" do
|
19
|
+
before do
|
20
|
+
::MongoMapper.connection.close
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should call reconnect" do
|
24
|
+
lambda do
|
25
|
+
Delayed::Backend::MongoMapper::Job.after_fork
|
26
|
+
end.should change { !!MongoMapper.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,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_mongo_mapper'
|
7
|
+
require 'delayed/backend/shared_spec'
|
8
|
+
|
9
|
+
MongoMapper.connection = Mongo::Connection.new('rails-mysql',nil)
|
10
|
+
MongoMapper.database = 'dl_spec'
|
11
|
+
|
12
|
+
class Story
|
13
|
+
include ::MongoMapper::Document
|
14
|
+
|
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,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leifcr-delayed_job_mongo_mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Timberlake
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongo_mapper
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.12'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.12'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: delayed_job
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.5
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.5
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
description:
|
63
|
+
email: leifcr@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
files:
|
70
|
+
- lib/delayed/backend/mongo_mapper.rb
|
71
|
+
- lib/delayed/serialization/mongo_mapper.rb
|
72
|
+
- lib/delayed_job_mongo_mapper.rb
|
73
|
+
- spec/delayed_job_mongo_mapper_spec.rb
|
74
|
+
- spec/spec.opts
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
homepage: http://github.com/leifcr/delayed_job_mongo_mapper
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- --charset=UTF-8
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
hash: -4411445201553814542
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
hash: -4411445201553814542
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.8.25
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: MongoMapper backend for delayed_job
|
109
|
+
test_files:
|
110
|
+
- spec/delayed_job_mongo_mapper_spec.rb
|
111
|
+
- spec/spec.opts
|
112
|
+
- spec/spec_helper.rb
|