delayed_job_couchrest_model 0.0.1

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 Nicholas Ricketts
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 couchrest_model backend
2
+
3
+ ## Installation
4
+
5
+ Add the gems to your Gemfile:
6
+
7
+ gem 'delayed_job'
8
+ gem 'delayed_job_couchrest_model'
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,83 @@
1
+ module Delayed
2
+ module Backend
3
+ module CouchRestModel
4
+ class Job < ::CouchRest::Model::Base
5
+ include Delayed::Backend::Base
6
+ use_database 'delayed_jobs'
7
+
8
+ property :handler
9
+ property :last_error
10
+ property :locked_by
11
+ property :priority, :default => 0
12
+ property :attempts, :default => 0
13
+ property :run_at, :type => Time
14
+ property :locked_at, :type => Time
15
+ property :failed_at, :type => Time
16
+ timestamps!
17
+
18
+ set_callback :save, :before, :set_default_run_at
19
+
20
+ view_by(:failed_at, :locked_by, :run_at,
21
+ :map => "function(doc){" +
22
+ " if(doc['type'] == 'Delayed::Backend::CouchRest::Job') {" +
23
+ " emit([doc.failed_at, doc.locked_by, doc.run_at], null);}" +
24
+ " }")
25
+ view_by(:failed_at, :locked_at, :run_at,
26
+ :map => "function(doc){" +
27
+ " if(doc['type'] == 'Delayed::Backend::CouchRest::Job') {" +
28
+ " emit([doc.failed_at, doc.locked_at, doc.run_at], null);}" +
29
+ " }")
30
+
31
+ def self.db_time_now; Time.now; end
32
+ def self.find_available(worker_name, limit = 5, max_run_time = ::Delayed::Worker.max_run_time)
33
+ ready = ready_jobs
34
+ mine = my_jobs worker_name
35
+ expire = expired_jobs max_run_time
36
+ jobs = (ready + mine + expire)[0..limit-1].sort_by { |j| j.priority }
37
+ jobs = jobs.find_all { |j| j.priority >= Worker.min_priority } if Worker.min_priority
38
+ jobs = jobs.find_all { |j| j.priority <= Worker.max_priority } if Worker.max_priority
39
+ jobs
40
+ end
41
+ def self.clear_locks!(worker_name)
42
+ jobs = my_jobs worker_name
43
+ jobs.each { |j| j.locked_by, j.locked_at = nil, nil; }
44
+ database.bulk_save jobs
45
+ end
46
+ def self.delete_all
47
+ database.bulk_save all.each { |doc| doc['_deleted'] = true }
48
+ end
49
+
50
+ def lock_exclusively!(max_run_time, worker = worker_name)
51
+ return false if locked_by_other?(worker) and not expired?(max_run_time)
52
+ case
53
+ when locked_by_me?(worker)
54
+ self.locked_at = self.class.db_time_now
55
+ when (unlocked? or (locked_by_other?(worker) and expired?(max_run_time)))
56
+ self.locked_at, self.locked_by = self.class.db_time_now, worker
57
+ end
58
+ save
59
+ rescue RestClient::Conflict
60
+ false
61
+ end
62
+
63
+ private
64
+ def self.ready_jobs
65
+ options = {:startkey => [nil, nil], :endkey => [nil, nil, db_time_now]}
66
+ by_failed_at_and_locked_by_and_run_at options
67
+ end
68
+ def self.my_jobs(worker_name)
69
+ options = {:startkey => [nil, worker_name], :endkey => [nil, worker_name, {}]}
70
+ by_failed_at_and_locked_by_and_run_at options
71
+ end
72
+ def self.expired_jobs(max_run_time)
73
+ options = {:startkey => [nil,'0'], :endkey => [nil, db_time_now - max_run_time, db_time_now]}
74
+ by_failed_at_and_locked_at_and_run_at options
75
+ end
76
+ def unlocked?; locked_by.nil?; end
77
+ def expired?(time); locked_at < self.class.db_time_now - time; end
78
+ def locked_by_me?(worker); not locked_by.nil? and locked_by == worker; end
79
+ def locked_by_other?(worker); not locked_by.nil? and locked_by != worker; end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,24 @@
1
+ class CouchRest::ExtendedDocument
2
+ yaml_as "tag:ruby.yaml.org,2002:CouchRest"
3
+
4
+ def reload
5
+ job = self.class.get self['_id']
6
+ job.each {|k,v| self[k] = v}
7
+ end
8
+ def self.find(id)
9
+ get id
10
+ end
11
+ def self.yaml_new(klass, tag, val)
12
+ klass.get(val['_id'])
13
+ end
14
+ def to_yaml_properties
15
+ ['@id']
16
+ end
17
+ def ==(other)
18
+ if other.is_a? ::CouchRest::ExtendedDocument
19
+ self['_id'] == other['_id']
20
+ else
21
+ super
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'couchrest_model'
2
+ require 'delayed_job'
3
+ require 'delayed/serialization/couch_rest'
4
+ require 'delayed/backend/couch_rest_model'
5
+
6
+ Delayed::Worker.backend = :couch_rest
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Delayed::Backend::CouchRest::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,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_couch_rest'
8
+ require 'delayed/backend/shared_spec'
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
13
+
14
+ DB = Delayed::Backend::CouchRest::Job.use_database(CouchRest::Server.new.database!('delayed_job_spec'))
15
+
16
+ class Story < CouchRest::ExtendedDocument
17
+ use_database DB
18
+ property :text
19
+
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,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_couchrest_model
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - GTen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-24 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: couchrest_model
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "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
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 1
43
+ segments:
44
+ - 2
45
+ - 1
46
+ version: "2.1"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 13
58
+ segments:
59
+ - 1
60
+ - 2
61
+ - 9
62
+ version: 1.2.9
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: A CouchRest::Model backend for delayed_job
66
+ email:
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.md
74
+ files:
75
+ - lib/delayed/backend/couch_rest_model.rb
76
+ - lib/delayed/serialization/couch_rest.rb
77
+ - lib/delayed_job_couchrest_model.rb
78
+ - spec/delayed_job_couch_rest_spec.rb
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ - LICENSE
82
+ - README.md
83
+ homepage: http://github.com/gten/delayed_job_couchrest_model
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options:
88
+ - --charset=UTF-8
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.7.2
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: A CouchRest::Model backend for delayed_job
116
+ test_files:
117
+ - spec/delayed_job_couch_rest_spec.rb
118
+ - spec/spec.opts
119
+ - spec/spec_helper.rb