inst-jobs 0.16.0 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20101216224513_create_delayed_jobs.rb +2 -0
  3. data/db/migrate/20110208031356_add_delayed_jobs_tag.rb +2 -0
  4. data/db/migrate/20110426161613_add_delayed_jobs_max_attempts.rb +2 -0
  5. data/db/migrate/20110516225834_add_delayed_jobs_strand.rb +2 -0
  6. data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +2 -0
  7. data/db/migrate/20110610213249_optimize_delayed_jobs.rb +2 -0
  8. data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +2 -0
  9. data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +2 -0
  10. data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +2 -0
  11. data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +2 -0
  12. data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +2 -0
  13. data/db/migrate/20120608191051_add_jobs_run_at_index.rb +2 -0
  14. data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +2 -0
  15. data/db/migrate/20140505215131_add_failed_jobs_original_job_id.rb +2 -0
  16. data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -0
  17. data/db/migrate/20140505223637_drop_failed_jobs_original_id.rb +2 -0
  18. data/db/migrate/20140512213941_add_source_to_jobs.rb +2 -0
  19. data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +2 -0
  20. data/db/migrate/20151123210429_add_expires_at_to_jobs.rb +2 -0
  21. data/db/migrate/20151210162949_improve_max_concurrent.rb +2 -0
  22. data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +2 -0
  23. data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +2 -0
  24. data/db/migrate/20190726154743_make_critical_columns_not_null.rb +2 -0
  25. data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +2 -0
  26. data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +2 -0
  27. data/db/migrate/20200825011002_add_strand_order_override.rb +2 -0
  28. data/lib/delayed/backend/active_record.rb +6 -4
  29. data/lib/delayed/backend/base.rb +34 -20
  30. data/lib/delayed/backend/redis/functions.rb +2 -0
  31. data/lib/delayed/backend/redis/job.rb +2 -0
  32. data/lib/delayed/batch.rb +5 -3
  33. data/lib/delayed/cli.rb +2 -0
  34. data/lib/delayed/core_ext/kernel.rb +9 -0
  35. data/lib/delayed/daemon.rb +2 -0
  36. data/lib/delayed/engine.rb +2 -0
  37. data/lib/delayed/job_tracking.rb +2 -0
  38. data/lib/delayed/lifecycle.rb +2 -0
  39. data/lib/delayed/log_tailer.rb +2 -0
  40. data/lib/delayed/logging.rb +2 -0
  41. data/lib/delayed/message_sending.rb +90 -106
  42. data/lib/delayed/performable_method.rb +35 -6
  43. data/lib/delayed/periodic.rb +6 -4
  44. data/lib/delayed/plugin.rb +2 -0
  45. data/lib/delayed/pool.rb +2 -0
  46. data/lib/delayed/server.rb +2 -0
  47. data/lib/delayed/server/helpers.rb +2 -0
  48. data/lib/delayed/settings.rb +2 -0
  49. data/lib/delayed/testing.rb +2 -0
  50. data/lib/delayed/version.rb +3 -1
  51. data/lib/delayed/work_queue/in_process.rb +2 -0
  52. data/lib/delayed/work_queue/parent_process.rb +2 -0
  53. data/lib/delayed/work_queue/parent_process/client.rb +2 -0
  54. data/lib/delayed/work_queue/parent_process/server.rb +2 -0
  55. data/lib/delayed/worker.rb +2 -0
  56. data/lib/delayed/worker/consul_health_check.rb +3 -1
  57. data/lib/delayed/worker/health_check.rb +2 -0
  58. data/lib/delayed/worker/null_health_check.rb +2 -0
  59. data/lib/delayed/worker/process_helper.rb +2 -0
  60. data/lib/delayed/yaml_extensions.rb +2 -0
  61. data/lib/delayed_job.rb +4 -0
  62. data/lib/inst-jobs.rb +2 -0
  63. data/spec/active_record_job_spec.rb +4 -6
  64. data/spec/delayed/cli_spec.rb +2 -0
  65. data/spec/delayed/daemon_spec.rb +2 -0
  66. data/spec/delayed/message_sending_spec.rb +101 -0
  67. data/spec/delayed/server_spec.rb +2 -4
  68. data/spec/delayed/settings_spec.rb +2 -0
  69. data/spec/delayed/work_queue/in_process_spec.rb +2 -4
  70. data/spec/delayed/work_queue/parent_process/client_spec.rb +2 -4
  71. data/spec/delayed/work_queue/parent_process/server_spec.rb +2 -1
  72. data/spec/delayed/work_queue/parent_process_spec.rb +2 -1
  73. data/spec/delayed/worker/consul_health_check_spec.rb +3 -1
  74. data/spec/delayed/worker/health_check_spec.rb +2 -0
  75. data/spec/delayed/worker_spec.rb +2 -0
  76. data/spec/migrate/20140924140513_add_story_table.rb +2 -0
  77. data/spec/redis_job_spec.rb +10 -12
  78. data/spec/sample_jobs.rb +2 -0
  79. data/spec/shared/delayed_batch.rb +17 -15
  80. data/spec/shared/delayed_method.rb +49 -204
  81. data/spec/shared/performable_method.rb +17 -9
  82. data/spec/shared/shared_backend.rb +27 -25
  83. data/spec/shared/testing.rb +7 -5
  84. data/spec/shared/worker.rb +15 -13
  85. data/spec/shared_jobs_specs.rb +2 -0
  86. data/spec/spec_helper.rb +12 -1
  87. metadata +19 -2
@@ -1,13 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
- class PerformableMethod < Struct.new(:object, :method, :args, :fail_cb, :permanent_fail_cb)
3
- def initialize(object, method, args = [], fail_cb = nil, permanent_fail_cb = nil)
4
+ class PerformableMethod < Struct.new(:object, :method, :args, :kwargs, :fail_cb, :permanent_fail_cb, :sender)
5
+ def initialize(object, method, args: [], kwargs: {}, on_failure: nil, on_permanent_failure: nil, sender: nil)
4
6
  raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method, true)
5
7
 
6
8
  self.object = object
7
9
  self.args = args
10
+ self.kwargs = kwargs
8
11
  self.method = method.to_sym
9
- self.fail_cb = fail_cb
10
- self.permanent_fail_cb = permanent_fail_cb
12
+ self.fail_cb = on_failure
13
+ self.permanent_fail_cb = on_permanent_failure
14
+ self.sender = sender
15
+ begin
16
+ YAML.load(YAML.dump(sender))
17
+ rescue
18
+ # if for some reason you can't dump the sender, just drop it
19
+ self.sender = nil
20
+ end
11
21
  end
12
22
 
13
23
  def display_name
@@ -20,7 +30,24 @@ module Delayed
20
30
  alias_method :tag, :display_name
21
31
 
22
32
  def perform
23
- object.send(method, *args)
33
+ kwargs = self.kwargs || {}
34
+
35
+ sender_is_object = sender == object
36
+ sender_is_class = sender.is_a?(object.class)
37
+
38
+ if sender.nil? || sender_is_object || sender_is_class && object.protected_methods.include?(method)
39
+ if kwargs.empty?
40
+ object.send(method, *args)
41
+ else
42
+ object.send(method, *args, **kwargs)
43
+ end
44
+ else
45
+ if kwargs.empty?
46
+ object.public_send(method, *args)
47
+ else
48
+ object.public_send(method, *args, **kwargs)
49
+ end
50
+ end
24
51
  end
25
52
 
26
53
  def on_failure(error)
@@ -46,7 +73,9 @@ module Delayed
46
73
 
47
74
  def full_name
48
75
  obj_name = object.is_a?(ActiveRecord::Base) ? "#{object.class}.find(#{object.id}).#{method}" : display_name
49
- "#{obj_name}(#{args.map { |a| deep_de_ar_ize(a) }.join(', ')})"
76
+ kgs = kwargs || {}
77
+ kwargs_str = kgs.map { |(k, v)| ", #{k}: #{deep_de_ar_ize(v)}"}.join("")
78
+ "#{obj_name}(#{args.map { |a| deep_de_ar_ize(a) }.join(', ')}#{kwargs_str})"
50
79
  end
51
80
  end
52
81
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fugit'
2
4
 
3
5
  module Delayed
@@ -47,10 +49,10 @@ class Periodic
47
49
  end
48
50
 
49
51
  def enqueue
50
- Delayed::Job.enqueue(self, @job_args.merge(:max_attempts => 1,
51
- :run_at => @cron.next_time(Delayed::Periodic.now).utc.to_time,
52
- :singleton => tag,
53
- on_conflict: :patient))
52
+ Delayed::Job.enqueue(self, **@job_args.merge(:max_attempts => 1,
53
+ :run_at => @cron.next_time(Delayed::Periodic.now).utc.to_time,
54
+ :singleton => tag,
55
+ on_conflict: :patient))
54
56
  end
55
57
 
56
58
  def perform
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
 
3
5
  module Delayed
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  class Pool
3
5
  include Delayed::Logging
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sinatra/base'
2
4
  require 'sinatra/json'
3
5
  require 'json'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  class Server
3
5
  module Helpers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'erb'
3
5
  require 'active_support/core_ext/hash/indifferent_access'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  module Testing
3
5
  def self.run_job(job)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
- VERSION = "0.16.0"
4
+ VERSION = "1.0.4"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  module WorkQueue
3
5
  # The simplest possible implementation of a WorkQueue -- just turns around and
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
2
4
  require 'socket'
3
5
  require 'timeout'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  module WorkQueue
3
5
  class ParentProcess
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  module WorkQueue
3
5
  class ParentProcess
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
 
3
5
  class TimeoutError < RuntimeError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'health_check'
2
4
  require_relative 'process_helper'
3
5
  require 'socket'
@@ -11,7 +13,7 @@ module Delayed
11
13
  DEFAULT_SERVICE_NAME = 'inst-jobs_worker'.freeze
12
14
  attr_reader :agent_client, :catalog_client
13
15
 
14
- def initialize(*args)
16
+ def initialize(*, **)
15
17
  super
16
18
  # Because we don't want the consul client to be a hard dependency we're
17
19
  # only requiring it once it's absolutely needed
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  class Worker
3
5
  class HealthCheck
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  class Worker
3
5
  class NullHealthCheck < HealthCheck
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  class Worker
3
5
  module ProcessHelper
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # New definitions for YAML to aid in serialization and deserialization of delayed jobs.
2
4
 
3
5
  require 'yaml'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Delayed
2
4
  MIN_PRIORITY = 0
3
5
  HIGH_PRIORITY = 0
@@ -17,6 +19,8 @@ require 'active_support/core_ext/module/attribute_accessors'
17
19
  require 'active_record'
18
20
  require 'after_transaction_commit'
19
21
 
22
+ require 'delayed/core_ext/kernel'
23
+
20
24
  require 'delayed/settings'
21
25
  require 'delayed/yaml_extensions'
22
26
 
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'delayed_job'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path("../spec_helper", __FILE__)
2
4
 
3
5
  describe 'Delayed::Backed::ActiveRecord::Job' do
@@ -5,10 +7,6 @@ describe 'Delayed::Backed::ActiveRecord::Job' do
5
7
  Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
6
8
  end
7
9
 
8
- after :all do
9
- Delayed.send(:remove_const, :Job)
10
- end
11
-
12
10
  before do
13
11
  Delayed::Testing.clear_all!
14
12
  end
@@ -17,7 +15,7 @@ describe 'Delayed::Backed::ActiveRecord::Job' do
17
15
 
18
16
  it "should recover as well as possible from a failure failing a job" do
19
17
  allow(Delayed::Job::Failed).to receive(:create).and_raise(RuntimeError)
20
- job = "test".send_later_enqueue_args :reverse, no_delay: true
18
+ job = "test".delay(ignore_transaction: true).reverse
21
19
  job_id = job.id
22
20
  proc { job.fail! }.should raise_error(RuntimeError)
23
21
  proc { Delayed::Job.find(job_id) }.should raise_error(ActiveRecord::RecordNotFound)
@@ -43,7 +41,7 @@ describe 'Delayed::Backed::ActiveRecord::Job' do
43
41
  it "should select the next job at random if enabled" do
44
42
  begin
45
43
  Delayed::Settings.select_random_from_batch = true
46
- 15.times { "test".send_later :length }
44
+ 15.times { "test".delay.length }
47
45
  founds = []
48
46
  15.times do
49
47
  job = Delayed::Job.get_and_lock_next_available('tester')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::CLI do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::Daemon do
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'debug_inspector'
5
+
6
+ RSpec.describe Delayed::MessageSending do
7
+ before do
8
+ allow(::Rails.env).to receive(:test?).and_return(true)
9
+ end
10
+
11
+ before (:all) do
12
+ class SpecClass
13
+ def call_private(**enqueue_args)
14
+ delay(**enqueue_args).private_method
15
+ end
16
+
17
+ def call_protected(**enqueue_args)
18
+ other = self.class.new
19
+ other.delay(**enqueue_args).protected_method
20
+ end
21
+
22
+ private
23
+
24
+ def private_method
25
+ end
26
+
27
+ protected
28
+
29
+ def protected_method
30
+ end
31
+ end
32
+ end
33
+
34
+ after(:all) do
35
+ Object.send(:remove_const, :SpecClass)
36
+ end
37
+
38
+ let(:klass) { SpecClass }
39
+
40
+ it "allows an object to send a private message to itself" do
41
+ job = klass.new.call_private(ignore_transaction: true)
42
+ job.invoke_job
43
+ end
44
+
45
+ it "allows an object to send a private message to itself synchronouosly" do
46
+ klass.new.call_private(synchronous: true)
47
+ end
48
+
49
+ it "warns about directly sending a private message asynchronously" do
50
+ expect { klass.new.delay.private_method }.to raise_error(NoMethodError)
51
+ end
52
+
53
+ it "warns about directly sending a private message synchronusly" do
54
+ expect { klass.new.delay(synchronous: true).private_method }.to raise_error(NoMethodError)
55
+ end
56
+
57
+ it "does not warn about directly sending a private message in production" do
58
+ allow(::Rails.env).to receive(:test?).and_return(false)
59
+ allow(::Rails.env).to receive(:development?).and_return(false)
60
+ klass.new.delay.private_method
61
+ end
62
+
63
+ it "does not warn about directly sending a private message synchronously in production" do
64
+ allow(::Rails.env).to receive(:test?).and_return(false)
65
+ allow(::Rails.env).to receive(:development?).and_return(false)
66
+ klass.new.delay(synchronous: true).private_method
67
+ end
68
+
69
+ it "allows an object to send a protected message to itself" do
70
+ job = klass.new.call_protected(ignore_transaction: true)
71
+ job.invoke_job
72
+ end
73
+
74
+ it "allows an object to send a protected message to itself synchronouosly" do
75
+ klass.new.call_protected(synchronous: true)
76
+ end
77
+
78
+ it "warns about directly sending a protected message asynchronously" do
79
+ expect { klass.new.delay.protected_method }.to raise_error(NoMethodError)
80
+ end
81
+
82
+ it "warns about directly sending a protected message synchronusly" do
83
+ expect { klass.new.delay(synchronous: true).protected_method }.to raise_error(NoMethodError)
84
+ end
85
+
86
+ it "doesn't explode if you can't dump the sender" do
87
+ klass = Class.new do
88
+ def delay_something
89
+ Kernel.delay.sleep(1)
90
+ end
91
+
92
+ def encode_with(encoder)
93
+ raise "yaml encoding failed"
94
+ end
95
+ end
96
+
97
+ obj = klass.new
98
+ expect { YAML.dump(obj) }.to raise_error("yaml encoding failed")
99
+ expect { obj.delay_something }.not_to raise_error
100
+ end
101
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'delayed/server'
3
5
 
@@ -18,10 +20,6 @@ RSpec.describe Delayed::Server, sinatra: true do
18
20
  Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
19
21
  end
20
22
 
21
- after :all do
22
- Delayed.send(:remove_const, :Job)
23
- end
24
-
25
23
  describe "get '/running'" do
26
24
  before do
27
25
  3.times do |i|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::Settings do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::WorkQueue::InProcess do
@@ -5,10 +7,6 @@ RSpec.describe Delayed::WorkQueue::InProcess do
5
7
  Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
6
8
  end
7
9
 
8
- after :all do
9
- Delayed.send(:remove_const, :Job)
10
- end
11
-
12
10
  after :each do
13
11
  Delayed::Worker.lifecycle.reset!
14
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::WorkQueue::ParentProcess::Client do
@@ -14,10 +16,6 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Client do
14
16
  Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
15
17
  end
16
18
 
17
- after :all do
18
- Delayed.send(:remove_const, :Job)
19
- end
20
-
21
19
  it 'marshals the given arguments to the server and returns the response' do
22
20
  expect(addrinfo).to receive(:connect).once.and_return(connection)
23
21
  expect(connection).to receive(:eof?).and_return(false)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
@@ -29,7 +31,6 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
29
31
  end
30
32
 
31
33
  after :all do
32
- Delayed.send(:remove_const, :Job)
33
34
  Delayed::Settings.parent_process = {}
34
35
  end
35
36