job_hunter 0.1.0 → 0.2.0

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: c244340865960e0c4717ffd181cc0e12f11b4ed1
4
- data.tar.gz: b714e3c2548e5303b23095470b2f8fd426b5cb6a
3
+ metadata.gz: d2b4465adfe13c33ce17ab91820f12af51f646c6
4
+ data.tar.gz: 1895c7dfc3ea369904d621fce89cea593f96ff9d
5
5
  SHA512:
6
- metadata.gz: 29221c28c88302be1a112924f2bfa5467101f39886ac7936ceba6b00ec3ffdf5ef95a33d3fa3c9dc9b5979735eaf2b59a38d9b883e32d098f3ffb7abb3a14e43
7
- data.tar.gz: c0777983d23b3d20c83328b0eafe5b2ab2702665f61c78e915094cf52b1496fc501331b877cb3f3072c60e32b4e35c0d45c28d8800629290124dc7d7f44913d0
6
+ metadata.gz: 424a79f337a54bb1f8f68cd6f0a8d190dd06b3b86cc8b79a925567a589596401a3ba2a222e87a26386bdf4f1c744e8494cf80f0ad75a2b076348ded39ef224d0
7
+ data.tar.gz: 09e41ede15f6743e121c4b575f3dbce3a3996c4a3460e53cd5144e9c057e16925afe128bfba44b8e690260d6158097af1f8a1aa7284b13a1b0b805ef6f5e61ff
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- job_hunter (0.0.0)
5
- delayed_job (>= 3.0, < 5.0)
4
+ job_hunter (0.2.0)
5
+ delayed_job_active_record (>= 3.0, < 5.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,2 +1,80 @@
1
- job_hunter
1
+ JobHunter
2
2
  ==========
3
+
4
+ JobHunter is a library that cuts down on boilerplate when creating and finding [Delayed::Job](https://github.com/collectiveidea/delayed_job) custom jobs.
5
+
6
+ Installation
7
+ -------
8
+
9
+ JobHunter supports DelayedJob 3.0+ with ActiveRecord as the backend. Just add `job_hunter` to your `Gemfile`.
10
+
11
+ Usage
12
+ -------
13
+
14
+ Add `extend JobHunter` at the top of a class you plan to use as a custom job. After this line, you can set defaults for this job with `run_at`, `priority`, `queue`, and `max_attempts`:
15
+
16
+
17
+ ```ruby
18
+ class KustomJob < Struct.new(:model_id, :details)
19
+ extend JobHunter
20
+
21
+ run_at -> { 24.hours.from.now }
22
+ priority 37
23
+ queue :sugar_glider
24
+ max_attempts 4
25
+
26
+ def perform
27
+ # do amazing things
28
+ end
29
+ end
30
+ ```
31
+
32
+ `run_at` accepts a Proc object. The value is evaluated when you enqueue a new custom job.
33
+
34
+ JobHunter also provides class methods to find, create, and destroy jobs:
35
+
36
+ ```ruby
37
+ model_id = 22
38
+ details = "You seem to a have a squid on your head."
39
+
40
+ KustomJob.create(model_id, details)
41
+
42
+ KustomJob.find(model_id, details)
43
+
44
+ KustomJob.find_or_create(model_id, details)
45
+
46
+ KustomJob.delete(model_id, details)
47
+
48
+ ```
49
+
50
+ Adding an index to `handler` in the `delayed_jobs` table is recommended if you plan on making regular use of `find` or `find_or_create`.
51
+
52
+ All of these methods will return a `Delayed::Job` if they find, create, or destroy a job and `nil` otherwise.
53
+
54
+ The `create` and `find_or_create` methods accept the same options hash as `Delayed::Job.enqueue`. Options passed in to these methods (`run_at`, `queue`, and `priority`) will override the defauls set in your custom job class. Options passed in to `find_or_create` will not affect a job already enqueued.
55
+
56
+ These class methods have corresponding instance methods:
57
+
58
+ ```ruby
59
+ model_id = 22
60
+ details = "You seem to a have a squid on your head."
61
+
62
+ custom_job = KustomJob.new model_id, details
63
+
64
+ job.find
65
+
66
+ job.create
67
+
68
+ job.find_or_create
69
+
70
+ job.delete
71
+ ```
72
+
73
+ Unlike the identically named class methods, `#create` and `#find_or_create` do not take an options hash. However, you can still override defaults by initializing your class by using `new_with_options`:
74
+
75
+ ```ruby
76
+ super_custom_job = KustomJob.new_with_options model_id, details, run_at: 12.minutes.from.now
77
+
78
+ super_custom_job.create
79
+ ```
80
+
data/job_hunter.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
20
  gem.require_paths = ['lib']
21
21
 
22
- gem.add_dependency 'delayed_job', ['>= 3.0', '< 5.0']
22
+ gem.add_dependency 'delayed_job_active_record', ['>= 3.0', '< 5.0']
23
23
 
24
24
  gem.add_development_dependency 'rake', '~> 10'
25
25
  gem.add_development_dependency 'rspec', '~> 3'
@@ -0,0 +1,23 @@
1
+ module JobHunter
2
+ def run_at(proc)
3
+ @run_at = proc
4
+ end
5
+
6
+ def priority(pri)
7
+ @priority = pri
8
+ end
9
+
10
+ def queue(queue)
11
+ @queue = queue
12
+ end
13
+
14
+ def max_attempts(value)
15
+ define_method(:max_attempts) { value }
16
+ end
17
+
18
+ def _defaults_
19
+ { priority: @priority,
20
+ run_at: @run_at.try(:call),
21
+ queue: @queue }.select { |k, v| v }
22
+ end
23
+ end
@@ -1,20 +1,52 @@
1
1
  module JobHunter
2
+ def self.extended(klass)
3
+ klass.include(InstanceMethods)
4
+ end
5
+
2
6
  def create(*args)
3
- options = args.last.is_a?(Hash) ? args.pop : {}
4
- Delayed::Job.enqueue(new(*args), options)
7
+ new_with_options(*args).create
5
8
  end
6
9
 
7
10
  def find(*args)
8
- handler = new(*args).to_yaml
9
- Delayed::Job.where(handler: handler).first
11
+ new_with_options(*args).find
10
12
  end
11
13
 
12
14
  def find_or_create(*args)
13
- job_args = args.last.is_a?(Hash) ? args[0..-2] : args
14
- find(*job_args) or create(*args)
15
+ new_with_options(*args).find_or_create
15
16
  end
16
17
 
17
18
  def destroy(*args)
18
- find(*args).try(:destroy)
19
+ new_with_options(*args).destroy
20
+ end
21
+
22
+ def new_with_options(*args)
23
+ options = args.extract_options!
24
+ job = new(*args)
25
+ job.define_singleton_method :_options_,
26
+ -> { self.class._defaults_.merge(options) }
27
+ job
28
+ end
29
+
30
+ module InstanceMethods
31
+ def find
32
+ Delayed::Job.where(handler: to_yaml).first
33
+ end
34
+
35
+ def create
36
+ Delayed::Job.enqueue(self, _options_)
37
+ end
38
+
39
+ def find_or_create
40
+ find || create
41
+ end
42
+
43
+ def destroy
44
+ find.try(:destroy)
45
+ end
46
+
47
+ private
48
+ def _options_
49
+ {}
50
+ end
19
51
  end
20
52
  end
@@ -1,3 +1,3 @@
1
1
  module JobHunter
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/job_hunter.rb CHANGED
@@ -1,4 +1,2 @@
1
+ require 'job_hunter/defaults'
1
2
  require 'job_hunter/dj_wrappers'
2
-
3
- module JobHunter
4
- end
@@ -5,6 +5,10 @@ class KustomJob < Struct.new(:model_id, :details)
5
5
 
6
6
  def perform
7
7
  end
8
+
9
+ def self.remove_method(sym)
10
+ define_method(name) { super }
11
+ end
8
12
  end
9
13
 
10
14
  describe JobHunter do
@@ -79,5 +83,77 @@ describe JobHunter do
79
83
  it 'returns nil if there is nothing to destory' do
80
84
  expect(KustomJob.destroy model_id, details).to be_nil
81
85
  end
82
- end
86
+ end
87
+
88
+ context '.max_attempts' do
89
+ let(:attempts) { 3 }
90
+
91
+ it 'sets max attempts in an instance method of the same name' do
92
+ kustom_job = KustomJob.new
93
+ expect {
94
+ KustomJob.max_attempts attempts
95
+ }.to change{kustom_job.try(:max_attempts)}.from(nil).to(attempts)
96
+ end
97
+
98
+ after do
99
+ KustomJob.remove_method :max_attempts
100
+ end
101
+ end
102
+
103
+ context '.run_at' do
104
+ let(:default_time) { -> { DateTime.parse('April 23, 2015 07:23') } }
105
+ let(:override) { 6.hours.from_now }
106
+
107
+ before do
108
+ KustomJob.run_at default_time
109
+ end
110
+
111
+ it 'gives jobs the default run_at time' do
112
+ job = KustomJob.create
113
+ expect(job.run_at).to eq(default_time.call)
114
+ end
115
+
116
+ it 'allows overriding the default time' do
117
+ job = KustomJob.create run_at: override
118
+ expect(job.run_at).to eq(override)
119
+ end
120
+ end
121
+
122
+ context '.priority' do
123
+ let(:default_priority) { 37 }
124
+ let(:override) { 13 }
125
+
126
+ before do
127
+ KustomJob.priority default_priority
128
+ end
129
+
130
+ it 'gives jobs the default priority' do
131
+ job = KustomJob.create
132
+ expect(job.priority).to eq(default_priority)
133
+ end
134
+
135
+ it 'allows overriding the default priority' do
136
+ job = KustomJob.create priority: override
137
+ expect(job.priority).to eq(override)
138
+ end
139
+ end
140
+
141
+ context '.queue' do
142
+ let(:default_queue) { 'sugar_glider' }
143
+ let(:override) { 'sugar_moo' }
144
+
145
+ before do
146
+ KustomJob.queue default_queue
147
+ end
148
+
149
+ it 'gives jobs the default queue' do
150
+ job = KustomJob.create
151
+ expect(job.queue).to eq(default_queue)
152
+ end
153
+
154
+ it 'allows overriding the default queue' do
155
+ job = KustomJob.create queue: override
156
+ expect(job.queue).to eq(override)
157
+ end
158
+ end
83
159
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: job_hunter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Moser
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2014-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: delayed_job
14
+ name: delayed_job_active_record
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -87,6 +87,7 @@ files:
87
87
  - Rakefile
88
88
  - job_hunter.gemspec
89
89
  - lib/job_hunter.rb
90
+ - lib/job_hunter/defaults.rb
90
91
  - lib/job_hunter/dj_wrappers.rb
91
92
  - lib/job_hunter/version.rb
92
93
  - spec/job_hunter_spec.rb