delayed_job_active_record_unique 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40c7c3aa6b67950fa13299fc54a60f37a2a2994c
|
4
|
+
data.tar.gz: 01397e6296e7eb44c21fa5e711f6ba8c80de05b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18c07e6a7045d68b629d3b169257b8320aa91c0915cca0e471b7286d63a877c7126ec77c8b4d86e30cffbe165b41d41002cf44540301bff5820fc81901da6eab
|
7
|
+
data.tar.gz: e82e17e69f2aa17af28c93b512089d3c84876ab88b1338575b813f67bdd8e5813634714275a63b338805c1eb97ff8f01b0990d9c1c1efa84781372a5dff4511a
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[](http://badge.fury.io/rb/delayed_job_active_record_unique)
|
1
2
|
[](https://travis-ci.org/rajiteh/delayed_job_active_record_unique)
|
2
3
|
[](https://coveralls.io/r/rajiteh/delayed_job_active_record_unique)
|
3
4
|
[](https://gemnasium.com/rajiteh/delayed_job_active_record_unique)
|
@@ -44,32 +45,75 @@ delayed_job table.
|
|
44
45
|
Uniqueness of the job can be specified by passing the key `unique_job` to the DelayedJob enqueue call.
|
45
46
|
|
46
47
|
```ruby
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
unique_job: {
|
49
|
+
attr: :id # Required. A method name in the object that will be called to obtain a uniquely identifiable value.
|
50
|
+
replace: true # Optional. Default = false.
|
51
|
+
# Default behaviour will not enqueue a job that already exists.
|
52
|
+
# On true, Any job that is already queued under this unique ID will be replaced by the current.
|
53
|
+
}
|
52
54
|
```
|
53
55
|
|
54
56
|
It is possible to just supply a `Symbol` to `unique_job` to set the attr value and proceed with default options.
|
55
57
|
|
58
|
+
|
56
59
|
*Note:* You must supply a `queue` name to enqueue options or via class method `queue_name` as the uniqueness is only global to the queue.
|
57
|
-
|
60
|
+
|
61
|
+
### Example
|
58
62
|
|
59
63
|
If using `#handle_asynchronously`
|
60
64
|
|
61
65
|
```ruby
|
62
|
-
|
66
|
+
handle_asynchronously :solr_index, queue: 'solr_indexing', priority: 50, unique_job: { attr: :id, replace: true }
|
63
67
|
```
|
64
68
|
|
65
69
|
If using a standalone class
|
66
70
|
|
67
71
|
```ruby
|
68
|
-
|
69
|
-
|
72
|
+
Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...'), queue: 'news_letters', unique_job: :get_id
|
73
|
+
#NewsLetterJob must have a 'get_id' method that will return a unique value to it's context.
|
70
74
|
```
|
71
75
|
|
76
|
+
### Using Procs
|
72
77
|
|
78
|
+
Version 0.0.2 supports Procs as 'attr' values. The proc will be evaluated with the payload sent as the first argument.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
unique_job: {
|
82
|
+
attr: Proc.new { |obj| "custom_attr_#{obj.id}" }
|
83
|
+
replace: true
|
84
|
+
}
|
85
|
+
# OR
|
86
|
+
unique_job: Proc.new { |obj| "custom_attr_#{obj.id}" }
|
87
|
+
```
|
88
|
+
|
89
|
+
### Standalone Classes
|
90
|
+
|
91
|
+
If not supplied as an option, Unique Job will look for methods `delayed_job` and `delayed_job_replace?` methods in the
|
92
|
+
enqueued object and configure it self accordingly.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class NewsletterJob < Struct.new(:some_object)
|
96
|
+
|
97
|
+
def unique_job
|
98
|
+
"#{some_object.length}_can_manipulate"
|
99
|
+
end
|
100
|
+
|
101
|
+
def unique_job_replace?
|
102
|
+
true
|
103
|
+
end
|
104
|
+
|
105
|
+
def queue_name
|
106
|
+
'new_letters_ftw'
|
107
|
+
end
|
108
|
+
|
109
|
+
def perform
|
110
|
+
#some stuff happening here
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...')
|
115
|
+
```
|
116
|
+
|
73
117
|
## Contributing
|
74
118
|
|
75
119
|
1. Fork it ( https://github.com/rajiteh/delayed_job_active_record_unique/fork )
|
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "delayed_job_active_record_unique"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.2"
|
8
8
|
spec.authors = ["Rajitha Perera"]
|
9
9
|
spec.email = ["rajiteh@gmail.com"]
|
10
10
|
spec.summary = %q{Extend DelayedJob to support unique job indentification.}
|
@@ -7,35 +7,55 @@ module Delayed
|
|
7
7
|
class UniqueJob
|
8
8
|
ERROR_NO_QUEUE = ":queue MUST be specified in the options or a method "\
|
9
9
|
"queue_name must exist in the object for unique_job enqueue."
|
10
|
-
ERROR_BAD_ARGS = "'unique_job' must be one of: Hash with 'attr'
|
11
|
-
"or non blank String
|
10
|
+
ERROR_BAD_ARGS = "'unique_job' must be one of: Hash with key 'attr', a Symbol "\
|
11
|
+
"or non blank String and must be supplied with enqueue options "\
|
12
|
+
"or provided via a method named `unique_job` in the enqueued class."
|
12
13
|
|
13
14
|
attr_accessor :opts
|
15
|
+
attr_accessor :key
|
16
|
+
|
14
17
|
def initialize(opts)
|
15
18
|
self.opts = opts
|
16
19
|
|
17
|
-
if
|
18
|
-
self.opts[:
|
19
|
-
|
20
|
+
if detect_unique_job!
|
21
|
+
uj = self.opts[:unique_job]
|
22
|
+
puts "UJ: #{uj.inspect}"
|
23
|
+
candidate = if uj.is_a?(Hash)
|
24
|
+
if uj[:attr].is_a?(Proc)
|
25
|
+
uj[:attr].call(opts[:payload_object])
|
26
|
+
else
|
27
|
+
get_value_of(uj[:attr]) unless uj[:attr].blank?
|
28
|
+
end
|
29
|
+
elsif (uj.is_a?(String) || uj.is_a?(Symbol))
|
30
|
+
get_value_of(uj) unless uj.blank?
|
31
|
+
elsif uj.is_a?(Proc)
|
32
|
+
uj.call(opts[:payload_object])
|
33
|
+
end
|
34
|
+
|
35
|
+
raise ArgumentError.new ERROR_NO_QUEUE unless detect_queue_name!
|
36
|
+
puts "CANDIDATE: #{candidate}"
|
37
|
+
raise ArgumentError.new ERROR_BAD_ARGS if candidate.blank?
|
38
|
+
|
39
|
+
self.key = "#{candidate}"
|
20
40
|
else
|
21
|
-
|
41
|
+
disable_unique_job!
|
22
42
|
end
|
23
43
|
end
|
24
44
|
|
25
|
-
def attr
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
45
|
+
def get_value_of(attr)
|
46
|
+
if opts[:payload_object].is_a? Delayed::PerformableMethod
|
47
|
+
opts[:payload_object].object.send(attr)
|
48
|
+
else
|
49
|
+
opts[:payload_object].send(attr)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def disable_unique_job!
|
54
|
+
self.key = false
|
35
55
|
end
|
36
56
|
|
37
57
|
def require_unique?
|
38
|
-
|
58
|
+
key != false
|
39
59
|
end
|
40
60
|
|
41
61
|
def replace_job?
|
@@ -53,24 +73,35 @@ module Delayed
|
|
53
73
|
end
|
54
74
|
|
55
75
|
def ready_to_run_uniquely
|
56
|
-
Delayed::Job.ready_to_run('', Delayed::Worker.max_run_time).where(:unique_id =>
|
76
|
+
Delayed::Job.ready_to_run('', Delayed::Worker.max_run_time).where(:unique_id => slugged_key)
|
57
77
|
end
|
58
78
|
|
59
|
-
def
|
60
|
-
@
|
61
|
-
"#{opts[:queue]}_#{opts[:payload_object].object.send(attr)}"
|
62
|
-
else
|
63
|
-
"#{opts[:queue]}_#{opts[:payload_object].send(attr)}"
|
64
|
-
end
|
79
|
+
def slugged_key
|
80
|
+
@slugged_key ||= "#{opts[:queue]}_#{key}"
|
65
81
|
end
|
66
82
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
70
|
-
|
83
|
+
def detect_unique_job!
|
84
|
+
self.opts.has_key?(:unique_job) ||
|
85
|
+
(opts[:payload_object].respond_to? :unique_job) &&
|
86
|
+
(opts[:unique_job] = { attr: Proc.new { opts[:payload_object].unique_job } } ) &&
|
87
|
+
(detect_unique_job_replace! || true)
|
88
|
+
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
def detect_unique_job_replace!
|
93
|
+
(opts[:payload_object].respond_to? :unique_job_replace?) &&
|
94
|
+
(opts[:unique_job][:replace] = opts[:payload_object].unique_job_replace?)
|
71
95
|
end
|
96
|
+
|
97
|
+
def detect_queue_name!
|
98
|
+
self.opts.has_key?(:queue) ||
|
99
|
+
(opts[:payload_object].respond_to? :queue_name) &&
|
100
|
+
(opts[:queue] = opts[:payload_object].queue_name)
|
101
|
+
end
|
102
|
+
|
72
103
|
def enqueue_opts
|
73
|
-
require_unique? ? opts.except(:unique_job).merge(unique_id:
|
104
|
+
require_unique? ? opts.except(:unique_job).merge(unique_id: slugged_key) : opts
|
74
105
|
end
|
75
106
|
|
76
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_job_active_record_unique
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rajitha Perera
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: delayed_job_active_record
|
@@ -74,7 +74,6 @@ files:
|
|
74
74
|
- lib/delayed/backend/base/class_methods/unique_job.rb
|
75
75
|
- lib/delayed_job_active_record_unique.rb
|
76
76
|
- lib/generators/delayed_job/active_record_unique_generator.rb
|
77
|
-
- lib/generators/delayed_job/next_migration_version.rb
|
78
77
|
- lib/generators/delayed_job/templates/unique_id.rb
|
79
78
|
homepage: http://github.com/rajiteh/delayed_job_active_record_unique
|
80
79
|
licenses:
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module DelayedJob
|
2
|
-
module NextMigrationVersion
|
3
|
-
# while methods have moved around this has been the implementation
|
4
|
-
# since ActiveRecord 3.0
|
5
|
-
def next_migration_number(dirname)
|
6
|
-
next_migration_number = current_migration_number(dirname) + 1
|
7
|
-
if ActiveRecord::Base.timestamped_migrations
|
8
|
-
[Time.now.utc.strftime("%Y%m%d%H%M%S"), format("%.14d", next_migration_number)].max
|
9
|
-
else
|
10
|
-
format("%.3d", next_migration_number)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|