delayed_job_couch_rest 1.0.1.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 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 backend
2
+
3
+ ## Installation
4
+
5
+ Add the gems to your Gemfile:
6
+
7
+ gem 'delayed_job', '2.1.0.pre2'
8
+ gem 'delayed_job_couch_rest', '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,83 @@
1
+ module Delayed
2
+ module Backend
3
+ module CouchRest
4
+ class Job < ::CouchRest::ExtendedDocument
5
+ include Delayed::Backend::Base
6
+ use_database ::CouchRest::Server.new.database('delayed_job')
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['couchrest-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['couchrest-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_extended_document'
2
+ require 'delayed_job'
3
+ require 'delayed/serialization/couch_rest'
4
+ require 'delayed/backend/couch_rest'
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,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_couch_rest
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 1
9
+ - rc
10
+ version: 1.0.1.rc
11
+ platform: ruby
12
+ authors:
13
+ - Nicholas Ricketts
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-06 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: couchrest_extended_document
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: delayed_job
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 2
42
+ - 1
43
+ version: "2.1"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 2
56
+ - 9
57
+ version: 1.2.9
58
+ type: :development
59
+ version_requirements: *id003
60
+ description:
61
+ email: nightshade427@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.md
69
+ files:
70
+ - lib/delayed/backend/couch_rest.rb
71
+ - lib/delayed/serialization/couch_rest.rb
72
+ - lib/delayed_job_couch_rest.rb
73
+ - spec/delayed_job_couch_rest_spec.rb
74
+ - spec/spec.opts
75
+ - spec/spec_helper.rb
76
+ - LICENSE
77
+ - README.md
78
+ has_rdoc: true
79
+ homepage: http://github.com/nightshade427/delayed_job_couch_rest
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">"
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 1
100
+ - 3
101
+ - 1
102
+ version: 1.3.1
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.3.6
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: CouchRest backend for delayed_job
110
+ test_files:
111
+ - spec/delayed_job_couch_rest_spec.rb
112
+ - spec/spec.opts
113
+ - spec/spec_helper.rb