delayed_job_data_mapper_ste 1.0.0

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, DateTime, :index => :run_at_priority
15
+ property :locked_at, DateTime, :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,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_data_mapper_ste
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Brandon Keepers
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-14 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: dm-core
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
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
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: dm-aggregates
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: delayed_job
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 2
69
+ - 1
70
+ version: "2.1"
71
+ type: :runtime
72
+ version_requirements: *id004
73
+ - !ruby/object:Gem::Dependency
74
+ name: rspec
75
+ prerelease: false
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 1
83
+ - 2
84
+ - 9
85
+ version: 1.2.9
86
+ type: :development
87
+ version_requirements: *id005
88
+ description:
89
+ email: brandon@collectiveidea.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files:
95
+ - LICENSE
96
+ - README.md
97
+ files:
98
+ - lib/delayed/backend/data_mapper.rb
99
+ - lib/delayed/serialization/data_mapper.rb
100
+ - lib/delayed_job_data_mapper.rb
101
+ - spec/delayed_job_data_mapper_spec.rb
102
+ - spec/spec.opts
103
+ - spec/spec_helper.rb
104
+ - LICENSE
105
+ - README.md
106
+ has_rdoc: true
107
+ homepage: http://github.com/collectiveidea/delayed_job_data_mapper
108
+ licenses: []
109
+
110
+ post_install_message:
111
+ rdoc_options:
112
+ - --charset=UTF-8
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: DataMapper backend for delayed_job
138
+ test_files:
139
+ - spec/delayed_job_data_mapper_spec.rb
140
+ - spec/spec.opts
141
+ - spec/spec_helper.rb