delayed_job_data_mapper 1.0.0.rc → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb3c7603f72adbb6055edf919f048e3620fba1b6
4
+ data.tar.gz: e1550974885a9268c8e218a2622caec0fa5d7365
5
+ SHA512:
6
+ metadata.gz: b102e87531a09b32698a2e76a26baf00fd3b8965783257f6be37b0b209e40d7a1ece7b6dff5145d2f681543a7a94358582a3fcd5e0ba90502492ec713080a5d1
7
+ data.tar.gz: d0a1cbc5cc189cdaf39cd713837303f10686acff6d4f822951a3a62838a143e425f3becfa170fc99691a1ffd188eac4614d1d022d03cd65e4fd1c159282f4190
File without changes
data/README.md CHANGED
@@ -2,14 +2,21 @@
2
2
 
3
3
  ## Installation
4
4
 
5
- Add the gems to you Gemfile:
5
+ Add the gems to your Gemfile:
6
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:
7
+ gem 'delayed_job', '2.1.0.pre2'
8
+ gem 'delayed_job_data_mapper', '1.0.0.rc'
9
+
10
+ If you're using dm-rails gem, add this to your environment.rb:
11
+
12
+ XXX::Application::configure do
13
+ config.after_initialize do
14
+ Delayed::Worker.backend.auto_upgrade!
15
+ end
16
+ end
17
+
18
+ Otherwise, add an initializer:
11
19
 
12
- # config/initializers/delayed_job.rb
13
20
  Delayed::Worker.backend.auto_upgrade!
14
21
 
15
22
  That's it. Use [delayed_job as normal](http://github.com/collectiveidea/delayed_job).
@@ -1,104 +1,125 @@
1
+ # encoding: utf-8
1
2
  module Delayed
2
3
  module Backend
3
4
  module DataMapper
4
5
  class Job
5
6
  include ::DataMapper::Resource
6
7
  include Delayed::Backend::Base
7
-
8
+
8
9
  storage_names[:default] = 'delayed_jobs'
9
-
10
+
10
11
  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
-
12
+ property :priority, Integer, :default => 0, :index => :run_at_priority
13
+ property :attempts, Integer, :default => 0
14
+ property :handler, Text, :lazy => false
15
+ property :run_at, DateTime, :index => :run_at_priority
16
+ property :locked_at, DateTime, :index => true, :lazy => false
17
+ property :locked_by, Text, :lazy => false
18
+ property :failed_at, DateTime, :lazy => false
19
+ property :last_error, Text, :lazy => false
20
+ property :queue, String
21
+
22
+ before :save, :set_default_run_at
23
+
20
24
  def self.db_time_now
21
- Time.now
25
+ Time.now.utc.to_datetime
26
+ end
27
+
28
+ def self.lockable(worker_name, max_run_time = Worker.max_run_time)
29
+ never_failed &
30
+ never_run &
31
+ (locked_by(worker_name) | expired(max_run_time))
32
+ end
33
+
34
+ def self.expired(max_run_time = Worker.max_run_time)
35
+ (
36
+ all(:locked_at => nil) | # never locked
37
+ all(:locked_at.lt => db_time_now - max_run_time) # lock expired
38
+ )
22
39
  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
40
 
28
- # respect priorities
41
+ def self.locked_by(worker_name)
42
+ all(:locked_by => worker_name)
43
+ end
44
+
45
+ def self.never_run
46
+ (all(:run_at => nil) | all(:run_at.lte => db_time_now))
47
+ end
48
+
49
+ def self.never_failed
50
+ all(:failed_at => nil)
51
+ end
52
+
53
+ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
54
+ simple_conditions = {:limit => limit, :order => [:priority.asc, :run_at.asc]}
29
55
  simple_conditions[:priority.gte] = Worker.min_priority if Worker.min_priority
30
56
  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 )
57
+ simple_conditions[:queue] = Worker.queues if Worker.queues.any?
58
+
59
+ lockable(worker_name, max_run_time).all(simple_conditions)
42
60
  end
43
-
61
+
44
62
  # When a worker is exiting, make sure we don't have any locked jobs.
45
63
  def self.clear_locks!(worker_name)
46
64
  all(:locked_by => worker_name).update(:locked_at => nil, :locked_by => nil)
47
65
  end
48
-
66
+
49
67
  # Lock this job for this worker.
50
68
  # Returns true if we have the lock, false otherwise.
51
69
  def lock_exclusively!(max_run_time, worker = worker_name)
52
-
53
70
  now = self.class.db_time_now
54
- overtime = now - max_run_time
55
-
71
+
56
72
  # FIXME - this is a bit gross
57
73
  # DM doesn't give us the number of rows affected by a collection update
58
74
  # 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
-
75
+ collection = if locked_by != worker
76
+ self.class.expired(max_run_time).never_run.all(:id => id)
77
+ else
78
+ self.class.locked_by(worker).all(:id => id)
79
+ end
80
+
63
81
  attributes = collection.model.new(:locked_at => now, :locked_by => worker).dirty_attributes
64
82
  affected_rows = self.repository.update(attributes, collection)
65
-
83
+
66
84
  if affected_rows == 1
67
- self.locked_at = now
68
- self.locked_by = worker
69
- return true
85
+ reload # pick up the updates above
86
+ true
70
87
  else
71
- return false
88
+ # does this mean > 1 was locked, or none?
89
+ false
72
90
  end
73
91
  end
74
-
92
+
93
+ def reschedule_at
94
+ payload_object.respond_to?(:reschedule_at) ?
95
+ payload_object.reschedule_at(self.class.db_time_now, attempts) :
96
+ self.class.db_time_now + ((attempts ** 4) + 5).seconds
97
+ end
98
+
75
99
  # these are common to the other backends, so we provide an implementation
76
100
  def self.delete_all
77
101
  Delayed::Job.auto_migrate!
78
102
  end
79
-
103
+
80
104
  def self.find id
81
105
  get id
82
106
  end
83
-
107
+
84
108
  def update_attributes(attributes)
85
109
  attributes.each do |k,v|
86
110
  self[k] = v
87
111
  end
88
112
  self.save
89
113
  end
90
-
91
-
92
- end
93
-
94
- class JobObserver
95
- include ::DataMapper::Observer
96
114
 
97
- observe Job
115
+ def reload(*args)
116
+ reset
117
+ super
118
+ end
98
119
 
99
- before :save do
100
- self.run_at ||= self.class.db_time_now
101
- end
120
+ def ==(other)
121
+ id == other.id
122
+ end
102
123
  end
103
124
  end
104
125
  end
@@ -1,11 +1,29 @@
1
- DataMapper::Resource.class_eval do
2
- yaml_as "tag:ruby.yaml.org,2002:DataMapper"
1
+ # encoding: utf-8
2
+ if YAML.parser.class.name =~ /syck/i
3
+ DataMapper::Resource.class_eval do
4
+ yaml_as "tag:ruby.yaml.org,2002:DataMapper"
3
5
 
4
- def self.yaml_new(klass, tag, val)
5
- klass.find(val['id'])
6
- end
6
+ def self.yaml_new(klass, tag, val)
7
+ begin
8
+ primary_keys = klass.properties.select { |p| p.key? }
9
+ key_names = primary_keys.map { |p| p.name.to_s }
10
+ klass.get!(*key_names.map { |k| val[k] })
11
+ rescue DataMapper::ObjectNotFoundError
12
+ raise Delayed::DeserializationError
13
+ end
14
+ end
15
+
16
+ def to_yaml_properties
17
+ primary_keys = self.class.properties.select { |p| p.key? }
18
+ primary_keys.map { |p| "@#{p.name}" }
19
+ end
7
20
 
8
- def to_yaml_properties
9
- ['@id']
21
+ end
22
+ else
23
+ DataMapper::Resource.class_eval do
24
+ def encode_with(coder)
25
+ coder["attributes"] = attributes.stringify_keys
26
+ coder.tag = ['!ruby/DataMapper', self.class.name].join(':')
27
+ end
10
28
  end
11
29
  end
@@ -1,6 +1,6 @@
1
+ # encoding: utf-8
1
2
  require 'dm-core'
2
- require 'dm-observer'
3
- require 'dm-aggregates'
3
+ require 'dm-active_model' # for persisted? alias for DJ 4.0.1
4
4
  require 'delayed_job'
5
5
  require 'delayed/serialization/data_mapper'
6
6
  require 'delayed/backend/data_mapper'
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'helper'
2
2
 
3
3
  describe Delayed::Backend::DataMapper::Job do
4
4
  it_should_behave_like 'a delayed_job backend'
@@ -1,16 +1,6 @@
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'
1
+ require 'rspec'
7
2
  require 'delayed_job_data_mapper'
8
3
  require 'delayed/backend/shared_spec'
9
-
10
- Spec::Runner.configure do |config|
11
-
12
- end
13
-
14
4
  require 'dm-migrations'
15
5
  DataMapper.setup(:default, "sqlite3::memory:")
16
6
 
@@ -18,9 +8,15 @@ class Story
18
8
  include DataMapper::Resource
19
9
  property :id, Serial
20
10
  property :text, String
21
- def tell; text; end
11
+ def tell; text; end
22
12
  def whatever(n, _); tell*n; end
23
13
  def self.count; end
14
+ def update_attributes(attributes)
15
+ attributes.each do |k,v|
16
+ self[k] = v
17
+ end
18
+ self.save
19
+ end
24
20
 
25
21
  handle_asynchronously :whatever
26
22
  end
metadata CHANGED
@@ -1,137 +1,181 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Brandon Keepers
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2010-09-05 00:00:00 -04:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
22
28
  name: dm-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
23
35
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dm-active_model
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
31
48
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: dm-observer
35
49
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- requirements:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
38
59
  - - ">="
39
- - !ruby/object:Gem::Version
40
- segments:
41
- - 0
42
- version: "0"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
43
62
  type: :runtime
44
- version_requirements: *id002
45
- - !ruby/object:Gem::Dependency
46
- name: dm-aggregates
47
63
  prerelease: false
48
- requirement: &id003 !ruby/object:Gem::Requirement
49
- requirements:
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: tzinfo
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
50
73
  - - ">="
51
- - !ruby/object:Gem::Version
52
- segments:
53
- - 0
54
- version: "0"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
55
76
  type: :runtime
56
- version_requirements: *id003
57
- - !ruby/object:Gem::Dependency
58
- name: delayed_job
59
77
  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
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
71
98
  name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: dm-migrations
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
72
119
  prerelease: false
73
- requirement: &id005 !ruby/object:Gem::Requirement
74
- requirements:
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
75
122
  - - ">="
76
- - !ruby/object:Gem::Version
77
- segments:
78
- - 1
79
- - 2
80
- - 9
81
- version: 1.2.9
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: dm-sqlite-adapter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
82
132
  type: :development
83
- version_requirements: *id005
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
84
139
  description:
85
140
  email: brandon@collectiveidea.com
86
141
  executables: []
87
-
88
142
  extensions: []
89
-
90
- extra_rdoc_files:
91
- - LICENSE
143
+ extra_rdoc_files:
144
+ - LICENSE.md
145
+ - README.md
146
+ files:
147
+ - LICENSE.md
92
148
  - README.md
93
- files:
94
149
  - lib/delayed/backend/data_mapper.rb
95
150
  - lib/delayed/serialization/data_mapper.rb
96
151
  - lib/delayed_job_data_mapper.rb
97
152
  - 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
153
+ - spec/helper.rb
103
154
  homepage: http://github.com/collectiveidea/delayed_job_data_mapper
104
- licenses: []
105
-
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
106
158
  post_install_message:
107
- rdoc_options:
108
- - --charset=UTF-8
109
- require_paths:
159
+ rdoc_options:
160
+ - "--charset=UTF-8"
161
+ require_paths:
110
162
  - lib
111
- required_ruby_version: !ruby/object:Gem::Requirement
112
- requirements:
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
113
170
  - - ">="
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
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
127
173
  requirements: []
128
-
129
174
  rubyforge_project:
130
- rubygems_version: 1.3.6
175
+ rubygems_version: 2.4.1
131
176
  signing_key:
132
- specification_version: 3
177
+ specification_version: 4
133
178
  summary: DataMapper backend for delayed_job
134
- test_files:
179
+ test_files:
135
180
  - spec/delayed_job_data_mapper_spec.rb
136
- - spec/spec.opts
137
- - spec/spec_helper.rb
181
+ - spec/helper.rb
@@ -1 +0,0 @@
1
- --color