canvas_sync 0.17.34 → 0.17.36

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: 8029fc55239810f8064f8d1c9fb4cd1f1756a7605581e7aced13e150e69c8b6b
4
- data.tar.gz: 2deac9eb222ed8c636159c8100e8aa6cd59d2fa26d3b13b83c7be31cf7cbc0bd
3
+ metadata.gz: 2bcfff804b06ecd1812adc2b4cc52ac50f925329cdfe196b1e734a7ab6f308f8
4
+ data.tar.gz: c553b8bdab0ba265e6d81f254c7d7022392a90a01af1bd998a81bdb36948d2b7
5
5
  SHA512:
6
- metadata.gz: b1d70b6a2493a49a9eeb48532e9438d02209d3de9609afa0a9bd09a5fe4c9816dcd9b3d7724ffa2385cd65f19b873901cce5fe5b733fa534e4c96ce785146f91
7
- data.tar.gz: c985b3da6ca96c4120b852f0b4df938a6393a797dd8365ce3c9e61404b67623484930b9001e13172309399e745774bd63208097954d569e094f86c4a56e94e38
6
+ metadata.gz: 4e2ac201fb1645f14cc82f3e8dda29640c7b82cbf9ff35b07d82ad898c926d2436694d4eb48fed3f7f528df4a33529e59304978f229e72ce9d469cc0d794b674
7
+ data.tar.gz: d6345cf78fe6f606d98d8aadfc0ab9e7ae9f7cfb4b3655fa274e7d74381b2ffb32295896569e13d79357a58254f365b1453cdeab98d543f855709b0072289da7
@@ -29,6 +29,21 @@ module CanvasSync
29
29
  }
30
30
  }
31
31
 
32
+ initializer 'canvas_sync.global_methods' do
33
+ next if defined?(canvas_sync_client)
34
+
35
+ require 'panda_pal'
36
+
37
+ def canvas_sync_client(account_id = nil)
38
+ Bearcat::Client.new(
39
+ prefix: current_organization.canvas_url,
40
+ token: current_organization.canvas_api_token,
41
+ master_rate_limit: (Rails.env.production? && !!defined?(Redis) && ENV['REDIS_URL'].present?),
42
+ )
43
+ end
44
+ rescue LoadError
45
+ end
46
+
32
47
  initializer :integrate_pandapal do
33
48
  require 'panda_pal'
34
49
 
@@ -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.smembers(key, 0, -1)
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"]}, callback batch id: #{callback_bid} callback_batch #{is_callback_batch}"}
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 msg['class'] == 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper' && (msg['wrapped'].to_s).constantize < BatchAwareJob
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)
@@ -1,3 +1,3 @@
1
1
  module CanvasSync
2
- VERSION = "0.17.34".freeze
2
+ VERSION = "0.17.36".freeze
3
3
  end