delayed_job_data_mapper 1.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
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,15 @@
1
+ # delayed_job DataMapper backend
2
+
3
+ ## Installation
4
+
5
+ Add the gems to you Gemfile:
6
+
7
+ gem 'delayed_job', '2.1.0.pre2'
8
+ gem 'delayed_job_data_mapper', '1.0.0.rc'
9
+
10
+ Add this in an initializer:
11
+
12
+ # config/initializers/delayed_job.rb
13
+ Delayed::Worker.backend.auto_upgrade!
14
+
15
+ That's it. Use [delayed_job as normal](http://github.com/collectiveidea/delayed_job).
@@ -0,0 +1,105 @@
1
+ module Delayed
2
+ module Backend
3
+ module DataMapper
4
+ class Job
5
+ include ::DataMapper::Resource
6
+ include Delayed::Backend::Base
7
+
8
+ storage_names[:default] = 'delayed_jobs'
9
+
10
+ property :id, Serial
11
+ property :priority, Integer, :default => 0, :index => :run_at_priority
12
+ property :attempts, Integer, :default => 0
13
+ property :handler, Text, :lazy => false
14
+ property :run_at, Time, :index => :run_at_priority
15
+ property :locked_at, Time, :index => true
16
+ property :locked_by, String
17
+ property :failed_at, Time
18
+ property :last_error, Text
19
+
20
+ def self.db_time_now
21
+ Time.now
22
+ end
23
+
24
+ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
25
+
26
+ simple_conditions = { :run_at.lte => db_time_now, :limit => limit, :failed_at => nil, :order => [:priority.asc, :run_at.asc] }
27
+
28
+ # respect priorities
29
+ simple_conditions[:priority.gte] = Worker.min_priority if Worker.min_priority
30
+ simple_conditions[:priority.lte] = Worker.max_priority if Worker.max_priority
31
+
32
+ # lockable
33
+ lockable = (
34
+ # not locked or past the max time
35
+ ( all(:locked_at => nil ) | all(:locked_at.lt => db_time_now - max_run_time)) |
36
+
37
+ # OR locked by our worker
38
+ all(:locked_by => worker_name))
39
+
40
+ # plus some other boring junk
41
+ (lockable).all( simple_conditions )
42
+ end
43
+
44
+ # When a worker is exiting, make sure we don't have any locked jobs.
45
+ def self.clear_locks!(worker_name)
46
+ all(:locked_by => worker_name).update(:locked_at => nil, :locked_by => nil)
47
+ end
48
+
49
+ # Lock this job for this worker.
50
+ # Returns true if we have the lock, false otherwise.
51
+ def lock_exclusively!(max_run_time, worker = worker_name)
52
+
53
+ now = self.class.db_time_now
54
+ overtime = now - max_run_time
55
+
56
+ # FIXME - this is a bit gross
57
+ # DM doesn't give us the number of rows affected by a collection update
58
+ # so we have to circumvent some niceness in DM::Collection here
59
+ collection = locked_by != worker ?
60
+ (self.class.all(:id => id, :run_at.lte => now) & ( self.class.all(:locked_at => nil) | self.class.all(:locked_at.lt => overtime) ) ) :
61
+ self.class.all(:id => id, :locked_by => worker)
62
+
63
+ attributes = collection.model.new(:locked_at => now, :locked_by => worker).dirty_attributes
64
+ affected_rows = self.repository.update(attributes, collection)
65
+
66
+ if affected_rows == 1
67
+ self.locked_at = now
68
+ self.locked_by = worker
69
+ return true
70
+ else
71
+ return false
72
+ end
73
+ end
74
+
75
+ # these are common to the other backends, so we provide an implementation
76
+ def self.delete_all
77
+ Delayed::Job.auto_migrate!
78
+ end
79
+
80
+ def self.find id
81
+ get id
82
+ end
83
+
84
+ def update_attributes(attributes)
85
+ attributes.each do |k,v|
86
+ self[k] = v
87
+ end
88
+ self.save
89
+ end
90
+
91
+
92
+ end
93
+
94
+ class JobObserver
95
+ include ::DataMapper::Observer
96
+
97
+ observe Job
98
+
99
+ before :save do
100
+ self.run_at ||= self.class.db_time_now
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,11 @@
1
+ DataMapper::Resource.class_eval do
2
+ yaml_as "tag:ruby.yaml.org,2002:DataMapper"
3
+
4
+ def self.yaml_new(klass, tag, val)
5
+ klass.find(val['id'])
6
+ end
7
+
8
+ def to_yaml_properties
9
+ ['@id']
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'dm-core'
2
+ require 'dm-observer'
3
+ require 'dm-aggregates'
4
+ require 'delayed_job'
5
+ require 'delayed/serialization/data_mapper'
6
+ require 'delayed/backend/data_mapper'
7
+
8
+ Delayed::Worker.backend = :data_mapper
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Delayed::Backend::DataMapper::Job do
4
+ it_should_behave_like 'a delayed_job backend'
5
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,29 @@
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_data_mapper'
8
+ require 'delayed/backend/shared_spec'
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
13
+
14
+ require 'dm-migrations'
15
+ DataMapper.setup(:default, "sqlite3::memory:")
16
+
17
+ class Story
18
+ include DataMapper::Resource
19
+ property :id, Serial
20
+ property :text, String
21
+ def tell; text; end
22
+ def whatever(n, _); tell*n; end
23
+ def self.count; end
24
+
25
+ handle_asynchronously :whatever
26
+ end
27
+
28
+ DataMapper.auto_migrate!
29
+
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_data_mapper
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: dm-core
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: dm-observer
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ - !ruby/object:Gem::Dependency
46
+ name: dm-aggregates
47
+ prerelease: false
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ type: :runtime
56
+ version_requirements: *id003
57
+ - !ruby/object:Gem::Dependency
58
+ name: delayed_job
59
+ prerelease: false
60
+ requirement: &id004 !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 2
66
+ - 1
67
+ version: "2.1"
68
+ type: :runtime
69
+ version_requirements: *id004
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ prerelease: false
73
+ requirement: &id005 !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 1
79
+ - 2
80
+ - 9
81
+ version: 1.2.9
82
+ type: :development
83
+ version_requirements: *id005
84
+ description:
85
+ email: brandon@collectiveidea.com
86
+ executables: []
87
+
88
+ extensions: []
89
+
90
+ extra_rdoc_files:
91
+ - LICENSE
92
+ - README.md
93
+ files:
94
+ - lib/delayed/backend/data_mapper.rb
95
+ - lib/delayed/serialization/data_mapper.rb
96
+ - lib/delayed_job_data_mapper.rb
97
+ - spec/delayed_job_data_mapper_spec.rb
98
+ - spec/spec.opts
99
+ - spec/spec_helper.rb
100
+ - LICENSE
101
+ - README.md
102
+ has_rdoc: true
103
+ homepage: http://github.com/collectiveidea/delayed_job_data_mapper
104
+ licenses: []
105
+
106
+ post_install_message:
107
+ rdoc_options:
108
+ - --charset=UTF-8
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">"
121
+ - !ruby/object:Gem::Version
122
+ segments:
123
+ - 1
124
+ - 3
125
+ - 1
126
+ version: 1.3.1
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.3.6
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: DataMapper backend for delayed_job
134
+ test_files:
135
+ - spec/delayed_job_data_mapper_spec.rb
136
+ - spec/spec.opts
137
+ - spec/spec_helper.rb