canvas_sync 0.17.35 → 0.17.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/canvas_sync/job_batches/active_job.rb +108 -0
- data/lib/canvas_sync/job_batches/batch.rb +7 -4
- data/lib/canvas_sync/job_batches/callback.rb +1 -13
- data/lib/canvas_sync/job_batches/sidekiq.rb +12 -1
- data/lib/canvas_sync/version.rb +1 -1
- data/spec/dummy/log/test.log +13058 -0
- data/spec/job_batching/active_job_spec.rb +107 -0
- data/spec/job_batching/batch_spec.rb +1 -1
- metadata +5 -5
- data/lib/canvas_sync/job_batches/batch_aware_job.rb +0 -63
- data/spec/job_batching/batch_aware_job_spec.rb +0 -101
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bcfff804b06ecd1812adc2b4cc52ac50f925329cdfe196b1e734a7ab6f308f8
|
4
|
+
data.tar.gz: c553b8bdab0ba265e6d81f254c7d7022392a90a01af1bd998a81bdb36948d2b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e2ac201fb1645f14cc82f3e8dda29640c7b82cbf9ff35b07d82ad898c926d2436694d4eb48fed3f7f528df4a33529e59304978f229e72ce9d469cc0d794b674
|
7
|
+
data.tar.gz: d6345cf78fe6f606d98d8aadfc0ab9e7ae9f7cfb4b3655fa274e7d74381b2ffb32295896569e13d79357a58254f365b1453cdeab98d543f855709b0072289da7
|
@@ -0,0 +1,108 @@
|
|
1
|
+
|
2
|
+
module CanvasSync
|
3
|
+
module JobBatches
|
4
|
+
module ActiveJob
|
5
|
+
module BatchAwareJob
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
around_perform do |job, block|
|
10
|
+
if (@bid) # This _must_ be @bid - not just bid
|
11
|
+
prev_batch = Thread.current[CURRENT_BATCH_THREAD_KEY]
|
12
|
+
begin
|
13
|
+
Thread.current[CURRENT_BATCH_THREAD_KEY] = Batch.new(@bid)
|
14
|
+
block.call
|
15
|
+
Thread.current[CURRENT_BATCH_THREAD_KEY].save_context_changes
|
16
|
+
Batch.process_successful_job(@bid, job_id)
|
17
|
+
rescue
|
18
|
+
Batch.process_failed_job(@bid, job_id)
|
19
|
+
raise
|
20
|
+
ensure
|
21
|
+
Thread.current[CURRENT_BATCH_THREAD_KEY] = prev_batch
|
22
|
+
end
|
23
|
+
else
|
24
|
+
block.call
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
around_enqueue do |job, block|
|
29
|
+
if (batch = Thread.current[CURRENT_BATCH_THREAD_KEY])
|
30
|
+
@bid = batch.bid
|
31
|
+
batch.increment_job_queue(job_id) if @bid
|
32
|
+
end
|
33
|
+
block.call
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def bid
|
38
|
+
@bid || Thread.current[CURRENT_BATCH_THREAD_KEY]&.bid
|
39
|
+
end
|
40
|
+
|
41
|
+
def batch
|
42
|
+
Thread.current[CURRENT_BATCH_THREAD_KEY]
|
43
|
+
end
|
44
|
+
|
45
|
+
def batch_context
|
46
|
+
batch&.context || {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def valid_within_batch?
|
50
|
+
batch.valid?
|
51
|
+
end
|
52
|
+
|
53
|
+
def serialize
|
54
|
+
super.tap do |data|
|
55
|
+
data['batch_id'] = @bid # This _must_ be @bid - not just bid
|
56
|
+
data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def deserialize(data)
|
61
|
+
super
|
62
|
+
@bid = data['batch_id']
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class ActiveJobCallbackWorker < ::ActiveJob::Base
|
67
|
+
include Batch::Callback::CallbackWorkerCommon
|
68
|
+
|
69
|
+
def self.enqueue_all(args, queue)
|
70
|
+
args.each do |arg_set|
|
71
|
+
set(queue: queue).perform_later(*arg_set)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.handle_job_death(job, error = nil)
|
77
|
+
if job.is_a?(Array)
|
78
|
+
event = ActiveSupport::Notifications::Event.new(*job)
|
79
|
+
payload = event.payload
|
80
|
+
job = payload[:job].serialize
|
81
|
+
error = payload[:error]
|
82
|
+
end
|
83
|
+
|
84
|
+
if job["job_id"].present? && job["batch_id"].present?
|
85
|
+
CanvasSync::JobBatches::Batch.process_dead_job(job['batch_id'], job['job_id'])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.configure
|
90
|
+
::ActiveJob::Base.include BatchAwareJob
|
91
|
+
|
92
|
+
begin
|
93
|
+
ActiveSupport::Notifications.subscribe "discard.active_job" do |*args|
|
94
|
+
handle_job_death(args)
|
95
|
+
end
|
96
|
+
|
97
|
+
ActiveSupport::Notifications.subscribe "retry_stopped.active_job" do |*args|
|
98
|
+
handle_job_death(args)
|
99
|
+
end
|
100
|
+
rescue => err
|
101
|
+
Rails.logger.warn(err)
|
102
|
+
end
|
103
|
+
|
104
|
+
Batch::Callback.worker_class ||= ActiveJobCallbackWorker
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -6,7 +6,6 @@ end
|
|
6
6
|
|
7
7
|
require_relative './redis_model'
|
8
8
|
require_relative './redis_script'
|
9
|
-
require_relative './batch_aware_job'
|
10
9
|
require_relative "./callback"
|
11
10
|
require_relative "./context_hash"
|
12
11
|
require_relative "./status"
|
@@ -465,7 +464,7 @@ module CanvasSync
|
|
465
464
|
when 'set'
|
466
465
|
r.smembers(key)
|
467
466
|
when 'zset'
|
468
|
-
r.
|
467
|
+
r.zrange(key, 0, -1)
|
469
468
|
end
|
470
469
|
end
|
471
470
|
end
|
@@ -481,8 +480,6 @@ module CanvasSync
|
|
481
480
|
end
|
482
481
|
end
|
483
482
|
end
|
484
|
-
|
485
|
-
ActiveJob::Base.include BatchAwareJob
|
486
483
|
end
|
487
484
|
end
|
488
485
|
|
@@ -491,3 +488,9 @@ if defined?(::Sidekiq)
|
|
491
488
|
require_relative './sidekiq'
|
492
489
|
CanvasSync::JobBatches::Sidekiq.configure
|
493
490
|
end
|
491
|
+
|
492
|
+
# Automatically integrate with ActiveJob if it is present.
|
493
|
+
if defined?(::ActiveJob)
|
494
|
+
require_relative './active_job'
|
495
|
+
CanvasSync::JobBatches::ActiveJob.configure
|
496
|
+
end
|
@@ -38,24 +38,12 @@ module CanvasSync
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
class ActiveJobCallbackWorker < ActiveJob::Base
|
42
|
-
include CallbackWorkerCommon
|
43
|
-
|
44
|
-
def self.enqueue_all(args, queue)
|
45
|
-
args.each do |arg_set|
|
46
|
-
set(queue: queue).perform_later(*arg_set)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
worker_class = ActiveJobCallbackWorker
|
52
|
-
|
53
41
|
class Finalize
|
54
42
|
def dispatch(status, opts)
|
55
43
|
bid = opts["bid"]
|
56
44
|
event = opts["event"].to_sym
|
57
45
|
|
58
|
-
Batch.logger.debug {"Finalize #{event} batch id: #{opts["bid"]}
|
46
|
+
Batch.logger.debug {"Finalize #{event} batch id: #{opts["bid"]}"}
|
59
47
|
|
60
48
|
batch_status = Status.new bid
|
61
49
|
send(event, bid, batch_status, batch_status.parent_bid)
|
@@ -49,7 +49,7 @@ module CanvasSync
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def should_handle_batch?(msg)
|
52
|
-
return false if
|
52
|
+
return false if JobBatches::Sidekiq.is_activejob_job?(msg)
|
53
53
|
true
|
54
54
|
end
|
55
55
|
end
|
@@ -75,6 +75,12 @@ module CanvasSync
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
def self.is_activejob_job?(msg)
|
79
|
+
return false unless defined?(ActiveJob)
|
80
|
+
|
81
|
+
msg['class'] == 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper' && (msg['wrapped'].to_s).constantize < JobBatches::ActiveJob::BatchAwareJob
|
82
|
+
end
|
83
|
+
|
78
84
|
def self.configure
|
79
85
|
if defined?(::Sidekiq::Batch) && ::Sidekiq::Batch != JobBatches::Batch
|
80
86
|
print "WARNING: Detected Sidekiq Pro or sidekiq-batch. CanvasSync JobBatches may not be fully compatible!"
|
@@ -98,6 +104,11 @@ module CanvasSync
|
|
98
104
|
end
|
99
105
|
|
100
106
|
config.death_handlers << ->(job, ex) do
|
107
|
+
if is_activejob_job?(job)
|
108
|
+
JobBatches::ActiveJob.handle_job_death(job["args"][0], ex)
|
109
|
+
return
|
110
|
+
end
|
111
|
+
|
101
112
|
return unless job['bid'].present?
|
102
113
|
|
103
114
|
if defined?(::Apartment)
|
data/lib/canvas_sync/version.rb
CHANGED