workhorse 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 713d9c117c06a2e448ec9b963b216df9e46741f81f7e5e80c62f4b3648b1d29b
4
- data.tar.gz: f625a84d3173812546d200feac847cbd16dcd800ee6d2f188e44302c79078211
3
+ metadata.gz: d44a186907b790e7c3841ebfa08c4d6adf15906276c85c562e18b77927198c4c
4
+ data.tar.gz: e9d2e237c2980ce22783cbf9f5352824dffe2fc2ac055499e9578c30a0b237f1
5
5
  SHA512:
6
- metadata.gz: 25818f59461494d50b08a00f863c81d48521998462643134454fac6b8ba7c6c7d2358213342373c01b4dbc5ef1dd4d02bdd561e1a1c9c5294d840095e1e5b931
7
- data.tar.gz: 833e6132e223e85060253f678109dad5624802ad163c990caae78fc8a6de44820318505c36bb5709e2f7fd591f4ce7ed71aa2ed2487b6de43d3cec17eeb085da
6
+ metadata.gz: 88f2d387aeedf2c8128bc0d733d764eb5094be3c458c1ee954578647203b7cbd4907ec0159e81e05a7426674776ee0a53b2b7355523ee67be1ac1dc42d331243
7
+ data.tar.gz: 5630d627210c264dd14c9b29a233631a99dfbd1fe1e49e64ffa2cfd2f0e1d71f63869153e175a437c0b07152278ed687a713f8d58e7450b6242fb4ed32a1ca17
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Workhorse Change log
2
2
 
3
+ ## 0.5.0 - 2019-05-22
4
+
5
+ * Added support for ActiveJob
6
+
3
7
  ## 0.4.0 - 2019-05-15
4
8
 
5
9
  * Added instruments for clearing DbJob data. (PR #17)
data/FAQ.md CHANGED
@@ -75,7 +75,3 @@ production mode.
75
75
  ## Why does workhorse not support timeouts?
76
76
 
77
77
  Generic timeout implementations are [a dangerous thing](http://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/) in Ruby. This is why we decided against providing this feature in Workhorse and recommend to implement the timeouts inside of your jobs - i.e. via network timeouts.
78
-
79
- ## Why is workhorse not integrated into Rails per default?
80
-
81
- We have submitted [a pull request](https://github.com/rails/rails/pull/31504) to Rails, but they stated that they won't add any new adapters to Rails. There are plans to integrate this on the Gem side only - stay tuned.
data/README.md CHANGED
@@ -345,13 +345,22 @@ Performing a reset will reset the job state to `waiting` and it will be
345
345
  processed again. All meta fields will be reset as well. See inline documentation
346
346
  of `Workhorse::DbJob#reset!` for more details.
347
347
 
348
- ## Frequently asked questions
349
348
 
350
- Please consult the [FAQ](FAQ.md).
349
+ ## Using workhorse with Rails / ActiveJob
350
+
351
+ While workhorse can be used though its custom interface as documented above, it
352
+ is also fully integrated into Rails using `ActiveJob`. See [documentation of
353
+ ActiveJob](https://edgeguides.rubyonrails.org/active_job_basics.html) for more
354
+ information on how to use it.
351
355
 
352
- ## Roadmap
356
+ To use workhorse as your ActiveJob backend, set the `queue_adapter` to
357
+ `workhorse`, either using `config.active_job.queue_adapter` in your application
358
+ configuration or else using `self.queue_adapter` in a job class inheriting from
359
+ `ActiveJob`. See ActiveJob documentation for more details.
353
360
 
354
- * [ ] ActiveJob integration for Rails
361
+ ## Frequently asked questions
362
+
363
+ Please consult the [FAQ](FAQ.md).
355
364
 
356
365
  ## Copyright
357
366
 
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ task :gemspec do
18
18
  spec.add_development_dependency 'mysql2'
19
19
  spec.add_development_dependency 'colorize'
20
20
  spec.add_development_dependency 'benchmark-ips'
21
+ spec.add_development_dependency 'activejob'
21
22
  spec.add_dependency 'activesupport'
22
23
  spec.add_dependency 'activerecord'
23
24
  spec.add_dependency 'schemacop', '~> 2.0'
@@ -30,7 +31,7 @@ end
30
31
  require 'rake/testtask'
31
32
 
32
33
  Rake::TestTask.new do |t|
33
- t.pattern = 'test/workhorse/**/*_test.rb'
34
+ t.pattern = 'test/**/*_test.rb'
34
35
  t.verbose = false
35
36
  t.libs << 'test/lib'
36
37
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -0,0 +1,22 @@
1
+ module ActiveJob
2
+ module QueueAdapters
3
+ # == Workhorse adapter for Active Job
4
+ #
5
+ # Workhorse is a multi-threaded job backend with database queuing for ruby.
6
+ # Jobs are persisted in the database using ActiveRecird.
7
+ # Read more about Workhorse {here}[https://github.com/sitrox/activejob].
8
+ #
9
+ # To use Workhorse, set the queue_adapter config to +:workhorse+.
10
+ #
11
+ # Rails.application.config.active_job.queue_adapter = :workhorse
12
+ class WorkhorseAdapter
13
+ def enqueue(job) #:nodoc:
14
+ Workhorse.enqueue_active_job(job)
15
+ end
16
+
17
+ def enqueue_at(job, timestamp) #:nodoc:
18
+ Workhorse.enqueue_active_job(job, timestamp)
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/workhorse.rb CHANGED
@@ -47,6 +47,7 @@ require 'workhorse/poller'
47
47
  require 'workhorse/pool'
48
48
  require 'workhorse/worker'
49
49
  require 'workhorse/jobs/run_rails_op'
50
+ require 'workhorse/jobs/run_active_job'
50
51
  require 'workhorse/jobs/cleanup_succeeded_jobs'
51
52
 
52
53
  # Daemon functionality is not available on java platforms
@@ -54,3 +55,7 @@ if RUBY_PLATFORM != 'java'
54
55
  require 'workhorse/daemon'
55
56
  require 'workhorse/daemon/shell_handler'
56
57
  end
58
+
59
+ if defined?(ActiveJob)
60
+ require 'active_job/queue_adapters/workhorse_adapter.rb'
61
+ end
@@ -11,8 +11,12 @@ module Workhorse
11
11
  end
12
12
 
13
13
  # Enqueue an ActiveJob job
14
- def enqueue_active_job(job)
15
- enqueue job, queue: job.queue_name, priority: job.priority
14
+ def enqueue_active_job(job, perform_at = Time.now)
15
+ wrapper_job = Jobs::RunActiveJob.new(job.serialize)
16
+ queue = job.queue_name.blank? ? nil : job.queue_name
17
+ db_job = enqueue(wrapper_job, queue: queue, priority: job.priority || 0, perform_at: Time.at(perform_at))
18
+ job.provider_job_id = db_job.id
19
+ return db_job
16
20
  end
17
21
 
18
22
  # Enqueue the execution of an operation by its class and params
@@ -0,0 +1,11 @@
1
+ module Workhorse::Jobs
2
+ class RunActiveJob
3
+ def initialize(job_data)
4
+ @job_data = job_data
5
+ end
6
+
7
+ def perform
8
+ ActiveJob::Base.execute(@job_data)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveJob::QueueAdapters::WorkhorseAdapterTest < WorkhorseTest
4
+ class ApplicationJob < ActiveJob::Base; end
5
+ class CustomException < StandardError; end
6
+
7
+ class Job1 < ApplicationJob
8
+ attr_reader :result
9
+
10
+ class_attribute :results
11
+
12
+ queue_as ''
13
+
14
+ def perform(param)
15
+ results << param
16
+ end
17
+ end
18
+
19
+ class Job2 < Job1
20
+ queue_as :queue1
21
+ end
22
+
23
+ class Job3 < Job1
24
+ rescue_from CustomException do |e|
25
+ results << e
26
+ end
27
+
28
+ def perform
29
+ fail CustomException
30
+ end
31
+ end
32
+
33
+ class Job4 < Job1
34
+ after_enqueue do |job|
35
+ results << job.provider_job_id
36
+ end
37
+ end
38
+
39
+ def setup
40
+ ActiveJob::Base.queue_adapter = :workhorse
41
+ ActiveJob::Base.logger = nil
42
+ Workhorse::DbJob.delete_all
43
+ Job1.results = Concurrent::Array.new
44
+ end
45
+
46
+ def test_basic
47
+ Job1.perform_later 'foo'
48
+ work 0.5
49
+ assert_equal ['foo'], Job1.results
50
+ assert_nil Workhorse::DbJob.first.queue
51
+ end
52
+
53
+ def test_queue
54
+ Job2.perform_later 'foo'
55
+ work 0.5
56
+ assert_equal ['foo'], Job2.results
57
+ assert_equal 'queue1', Workhorse::DbJob.first.queue
58
+ end
59
+
60
+ def test_wait
61
+ Job2.set(wait: 2.seconds).perform_later 'foo'
62
+
63
+ work 1, polling_interval: 0.1
64
+ assert_equal 'waiting', Workhorse::DbJob.first.state
65
+
66
+ work 2.5, polling_interval: 0.1
67
+ assert_equal 'succeeded', Workhorse::DbJob.first.reload.state
68
+ end
69
+
70
+ def test_wait_until
71
+ Job2.set(wait_until: (Time.now + 2.seconds)).perform_later 'foo'
72
+
73
+ work 0.5, polling_interval: 0.1
74
+ assert_equal 'waiting', Workhorse::DbJob.first.state
75
+
76
+ work 1.5, polling_interval: 0.1
77
+ assert_equal 'succeeded', Workhorse::DbJob.first.reload.state
78
+ end
79
+
80
+ def test_rescue_from
81
+ Job3.perform_later
82
+ work 0.5
83
+ assert_equal 'succeeded', Workhorse::DbJob.first.state
84
+ assert Job3.results.first.is_a?(CustomException)
85
+ end
86
+
87
+ def test_provider_job_id
88
+ job = Job4.perform_later
89
+ db_job = Workhorse::DbJob.first
90
+ assert_equal db_job.id, job.provider_job_id
91
+ assert_equal [db_job.id], Job1.results
92
+ end
93
+ end
@@ -1,5 +1,6 @@
1
1
  require 'minitest/autorun'
2
2
  require 'active_record'
3
+ require 'active_job'
3
4
  require 'mysql2'
4
5
  require 'benchmark'
5
6
  require 'jobs'
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class Workhorse::WorkerTest < WorkhorseTest
3
+ class Workhorse::PerformerTest < WorkhorseTest
4
4
  # This test makes sure that concurrent jobs always work in different database
5
5
  # connections.
6
6
  def test_db_connections
data/workhorse.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: workhorse 0.4.0 ruby lib
2
+ # stub: workhorse 0.5.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "workhorse".freeze
6
- s.version = "0.4.0"
6
+ s.version = "0.5.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Sitrox".freeze]
11
- s.date = "2019-05-15"
12
- s.files = [".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".travis.yml".freeze, "CHANGELOG.md".freeze, "FAQ.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "bin/rubocop".freeze, "lib/generators/workhorse/install_generator.rb".freeze, "lib/generators/workhorse/templates/bin/workhorse.rb".freeze, "lib/generators/workhorse/templates/config/initializers/workhorse.rb".freeze, "lib/generators/workhorse/templates/create_table_jobs.rb".freeze, "lib/workhorse.rb".freeze, "lib/workhorse/daemon.rb".freeze, "lib/workhorse/daemon/shell_handler.rb".freeze, "lib/workhorse/db_job.rb".freeze, "lib/workhorse/enqueuer.rb".freeze, "lib/workhorse/jobs/cleanup_succeeded_jobs.rb".freeze, "lib/workhorse/jobs/run_rails_op.rb".freeze, "lib/workhorse/performer.rb".freeze, "lib/workhorse/poller.rb".freeze, "lib/workhorse/pool.rb".freeze, "lib/workhorse/worker.rb".freeze, "test/lib/db_schema.rb".freeze, "test/lib/jobs.rb".freeze, "test/lib/test_helper.rb".freeze, "test/workhorse/db_job_test.rb".freeze, "test/workhorse/enqueuer_test.rb".freeze, "test/workhorse/performer_test.rb".freeze, "test/workhorse/poller_test.rb".freeze, "test/workhorse/pool_test.rb".freeze, "test/workhorse/worker_test.rb".freeze, "workhorse.gemspec".freeze]
11
+ s.date = "2019-05-22"
12
+ s.files = [".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".travis.yml".freeze, "CHANGELOG.md".freeze, "FAQ.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "bin/rubocop".freeze, "lib/active_job/queue_adapters/workhorse_adapter.rb".freeze, "lib/generators/workhorse/install_generator.rb".freeze, "lib/generators/workhorse/templates/bin/workhorse.rb".freeze, "lib/generators/workhorse/templates/config/initializers/workhorse.rb".freeze, "lib/generators/workhorse/templates/create_table_jobs.rb".freeze, "lib/workhorse.rb".freeze, "lib/workhorse/daemon.rb".freeze, "lib/workhorse/daemon/shell_handler.rb".freeze, "lib/workhorse/db_job.rb".freeze, "lib/workhorse/enqueuer.rb".freeze, "lib/workhorse/jobs/cleanup_succeeded_jobs.rb".freeze, "lib/workhorse/jobs/run_active_job.rb".freeze, "lib/workhorse/jobs/run_rails_op.rb".freeze, "lib/workhorse/performer.rb".freeze, "lib/workhorse/poller.rb".freeze, "lib/workhorse/pool.rb".freeze, "lib/workhorse/worker.rb".freeze, "test/active_job/queue_adapters/workhorse_adapter_test.rb".freeze, "test/lib/db_schema.rb".freeze, "test/lib/jobs.rb".freeze, "test/lib/test_helper.rb".freeze, "test/workhorse/db_job_test.rb".freeze, "test/workhorse/enqueuer_test.rb".freeze, "test/workhorse/performer_test.rb".freeze, "test/workhorse/poller_test.rb".freeze, "test/workhorse/pool_test.rb".freeze, "test/workhorse/worker_test.rb".freeze, "workhorse.gemspec".freeze]
13
13
  s.rubygems_version = "3.0.3".freeze
14
14
  s.summary = "Multi-threaded job backend with database queuing for ruby.".freeze
15
- s.test_files = ["test/lib/db_schema.rb".freeze, "test/lib/jobs.rb".freeze, "test/lib/test_helper.rb".freeze, "test/workhorse/db_job_test.rb".freeze, "test/workhorse/enqueuer_test.rb".freeze, "test/workhorse/performer_test.rb".freeze, "test/workhorse/poller_test.rb".freeze, "test/workhorse/pool_test.rb".freeze, "test/workhorse/worker_test.rb".freeze]
15
+ s.test_files = ["test/active_job/queue_adapters/workhorse_adapter_test.rb".freeze, "test/lib/db_schema.rb".freeze, "test/lib/jobs.rb".freeze, "test/lib/test_helper.rb".freeze, "test/workhorse/db_job_test.rb".freeze, "test/workhorse/enqueuer_test.rb".freeze, "test/workhorse/performer_test.rb".freeze, "test/workhorse/poller_test.rb".freeze, "test/workhorse/pool_test.rb".freeze, "test/workhorse/worker_test.rb".freeze]
16
16
 
17
17
  if s.respond_to? :specification_version then
18
18
  s.specification_version = 4
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency(%q<mysql2>.freeze, [">= 0"])
26
26
  s.add_development_dependency(%q<colorize>.freeze, [">= 0"])
27
27
  s.add_development_dependency(%q<benchmark-ips>.freeze, [">= 0"])
28
+ s.add_development_dependency(%q<activejob>.freeze, [">= 0"])
28
29
  s.add_runtime_dependency(%q<activesupport>.freeze, [">= 0"])
29
30
  s.add_runtime_dependency(%q<activerecord>.freeze, [">= 0"])
30
31
  s.add_runtime_dependency(%q<schemacop>.freeze, ["~> 2.0"])
@@ -37,6 +38,7 @@ Gem::Specification.new do |s|
37
38
  s.add_dependency(%q<mysql2>.freeze, [">= 0"])
38
39
  s.add_dependency(%q<colorize>.freeze, [">= 0"])
39
40
  s.add_dependency(%q<benchmark-ips>.freeze, [">= 0"])
41
+ s.add_dependency(%q<activejob>.freeze, [">= 0"])
40
42
  s.add_dependency(%q<activesupport>.freeze, [">= 0"])
41
43
  s.add_dependency(%q<activerecord>.freeze, [">= 0"])
42
44
  s.add_dependency(%q<schemacop>.freeze, ["~> 2.0"])
@@ -50,6 +52,7 @@ Gem::Specification.new do |s|
50
52
  s.add_dependency(%q<mysql2>.freeze, [">= 0"])
51
53
  s.add_dependency(%q<colorize>.freeze, [">= 0"])
52
54
  s.add_dependency(%q<benchmark-ips>.freeze, [">= 0"])
55
+ s.add_dependency(%q<activejob>.freeze, [">= 0"])
53
56
  s.add_dependency(%q<activesupport>.freeze, [">= 0"])
54
57
  s.add_dependency(%q<activerecord>.freeze, [">= 0"])
55
58
  s.add_dependency(%q<schemacop>.freeze, ["~> 2.0"])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workhorse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-15 00:00:00.000000000 Z
11
+ date: 2019-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activejob
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: activesupport
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +197,7 @@ files:
183
197
  - Rakefile
184
198
  - VERSION
185
199
  - bin/rubocop
200
+ - lib/active_job/queue_adapters/workhorse_adapter.rb
186
201
  - lib/generators/workhorse/install_generator.rb
187
202
  - lib/generators/workhorse/templates/bin/workhorse.rb
188
203
  - lib/generators/workhorse/templates/config/initializers/workhorse.rb
@@ -193,11 +208,13 @@ files:
193
208
  - lib/workhorse/db_job.rb
194
209
  - lib/workhorse/enqueuer.rb
195
210
  - lib/workhorse/jobs/cleanup_succeeded_jobs.rb
211
+ - lib/workhorse/jobs/run_active_job.rb
196
212
  - lib/workhorse/jobs/run_rails_op.rb
197
213
  - lib/workhorse/performer.rb
198
214
  - lib/workhorse/poller.rb
199
215
  - lib/workhorse/pool.rb
200
216
  - lib/workhorse/worker.rb
217
+ - test/active_job/queue_adapters/workhorse_adapter_test.rb
201
218
  - test/lib/db_schema.rb
202
219
  - test/lib/jobs.rb
203
220
  - test/lib/test_helper.rb
@@ -231,6 +248,7 @@ signing_key:
231
248
  specification_version: 4
232
249
  summary: Multi-threaded job backend with database queuing for ruby.
233
250
  test_files:
251
+ - test/active_job/queue_adapters/workhorse_adapter_test.rb
234
252
  - test/lib/db_schema.rb
235
253
  - test/lib/jobs.rb
236
254
  - test/lib/test_helper.rb