joblin 0.1.2 → 0.1.4

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
  SHA256:
3
- metadata.gz: a8da74810ea7a5b63274a757c844947d8f9c70d2a04bd69c43ecc25d28ad53fc
4
- data.tar.gz: c13aeaf271018036449c8cde653275bde37a6b383b46cef7e558a044b090a9ad
3
+ metadata.gz: c23bb12d2e39399c6211b0ed5bbfecf1b2b7aa538cd2d5ba252ea9621bf81ec1
4
+ data.tar.gz: ccaff17b2024b75ea3084f664e49b45701200e69377e2c6ffe1ed9f6348778e9
5
5
  SHA512:
6
- metadata.gz: 69259dcda6c3b247fe1822744b99d1e8f8316d9e930ab6c5d7a5290d0cf9c09fa90d0bd8759831bb325ff2c5f0647a6337009efea5f174234ae5d91e04dd4a76
7
- data.tar.gz: 70b002dd9071babd100fa3b5e024839b41f09e5dbd2c43fd619487d8c0e1c77502b87bb980593dabe33e9d17e9e74087401c40b874bf40de8a22f8100dc32f19
6
+ metadata.gz: b4902bf5d9a9a46d8b3933df45c9c23e2b2571c43202244a25a408d24a14079e4bf331997c53ea174a673646775de2beeac7a872f3e0234c68090c052609baf9
7
+ data.tar.gz: 84a99c0d34a9b11a6cfc6a342d7aba127126347d7d4fbc2be58e2ac58a71b383c132ee417bfc154baee77af300a909ac946ed771e9aa5b773fe8add5b27ef23f
@@ -16,7 +16,7 @@ module Joblin
16
16
  end
17
17
 
18
18
  def allow_api_access!(&blk)
19
- include ApiAccess::Mixin unless self < ApiAccess::Mixin
19
+ include Mixin unless self < Mixin
20
20
  @api_access_allowed = true
21
21
  api_access_rules(&blk) if blk
22
22
  end
@@ -28,7 +28,7 @@ module Joblin
28
28
 
29
29
  def find_for_api(id)
30
30
  task = find(id)
31
- raise ActiveRecord::RecordNotFound unless task.class.api_access_allowed?
31
+ raise ActiveRecord::RecordNotFound unless task.api_access_allowed?
32
32
  task
33
33
  end
34
34
 
@@ -42,9 +42,9 @@ module Joblin
42
42
 
43
43
  task_type = self == BackgroundTask ? params[:type].safe_constantize : self
44
44
  raise ActiveRecord::RecordNotFound unless task_type && task_type <= BackgroundTask
45
- raise ActiveRecord::RecordNotFound unless task_type.api_access_allowed?
46
45
 
47
46
  task = task_type.new
47
+ raise ActiveRecord::RecordNotFound unless task.api_access_allowed?
48
48
  rule_sets = task_type.api_access_rules
49
49
 
50
50
  # Apply permitted options
@@ -64,6 +64,8 @@ module Joblin
64
64
  # Apply any additional/hard-coded options
65
65
  task.options.merge!(options)
66
66
 
67
+ raise ActiveRecord::RecordInvalid.new(task) unless task.api_access_allowed?
68
+
67
69
  val_errors = task.api_validate_options
68
70
  raise ActiveRecord::ValidationError.new(val_errors) unless val_errors.empty?
69
71
 
@@ -77,14 +79,20 @@ module Joblin
77
79
  end
78
80
  end
79
81
 
82
+ def api_access_allowed?
83
+ self.class.api_access_allowed?
84
+ end
85
+
80
86
  module Mixin
81
87
  extend ActiveSupport::Concern
82
88
 
83
89
  included do
84
- if defined?(BackgroundTaskChannel)
90
+ begin
91
+ ::BackgroundTaskChannel
85
92
  after_commit do
86
- BackgroundTaskChannel.broadcast_to(self, api_serialize)
93
+ ::BackgroundTaskChannel.broadcast_to(self, api_serialize) if api_access_allowed?
87
94
  end
95
+ rescue NameError
88
96
  end
89
97
  end
90
98
 
@@ -2,6 +2,8 @@ module Joblin
2
2
  module BackgroundTask::Attachments
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ include Concerns::JobWorkingDirs
6
+
5
7
  def attachment_path(key, expires_in: true)
6
8
  if !expires_in || Rails.env.development? || Rails.env.test?
7
9
  return Rails.application.routes.url_helpers.rails_blob_path(
@@ -19,10 +19,26 @@ module Joblin
19
19
  delegate :queue_as, :sidekiq_options, to: :job_executor_class
20
20
 
21
21
  %i[before around after].each do |hook_type|
22
- define_method(:"#{hook_type}_perform") do |*args, &blk|
23
- blk ||= args[0].to_proc
24
- job_executor_class.send(:"#{hook_type}_perform") do |*args|
25
- the_task.instance_exec(&blk)
22
+ define_method(:"#{hook_type}_perform") do |*args, &callback|
23
+ callback ||= args[0]
24
+ if callback.is_a?(Symbol) || callback.is_a?(String)
25
+ job_executor_class.define_method(callback) do |*margs, &blk|
26
+ the_task.send(callback, *margs, &blk)
27
+ end
28
+ job_executor_class.send(:"#{hook_type}_perform", callback)
29
+ elsif callback.is_a?(Proc)
30
+ # Not exactly pretty...
31
+ proc = case callback.arity
32
+ when 0
33
+ -> { the_task.instance_exec(&callback) }
34
+ when 1
35
+ ->(a) { the_task.instance_exec(a, &callback) }
36
+ when 2
37
+ ->(a, b) { the_task.instance_exec(a, b, &callback) }
38
+ else
39
+ raise ArgumentError, "Unsupported callback arity #{callback.arity}"
40
+ end
41
+ job_executor_class.send(:"#{hook_type}_perform", &proc)
26
42
  end
27
43
  end
28
44
  end
@@ -6,6 +6,13 @@ module Joblin
6
6
  def bt_passthrough_values
7
7
  @bt_passthrough_values ||= (instance_methods - ActiveRecord::Base.instance_methods - %i[options= batch_id= id_value= type=]).map(&:to_s).select { |m| m.end_with?('=') }.map { |m| m[0..-2] }
8
8
  end
9
+
10
+ def new(*args, **kwargs, &blk)
11
+ options = kwargs.delete(:options) || {}
12
+ super.tap do |instance|
13
+ instance.options.merge!(options)
14
+ end
15
+ end
9
16
  end
10
17
 
11
18
  included do
@@ -34,6 +34,10 @@ module Joblin
34
34
  update!(workflow_state: 'cancelled')
35
35
  end
36
36
 
37
+ def handle_batch_stagnation
38
+ Joblin::Batching::Batch.cleanup_redis(batch_id)
39
+ end
40
+
37
41
  protected
38
42
 
39
43
  def perform
@@ -46,6 +50,7 @@ module Joblin
46
50
  b.description = "BackgroundTask #{id}"
47
51
  b.on(:complete, BackgroundTaskCallbacks, id: id)
48
52
  b.on(:success, BackgroundTaskCallbacks, id: id)
53
+ b.on(:stagnated, BackgroundTaskCallbacks, id: id)
49
54
  b.context[:background_task_id] = id
50
55
  end
51
56
  b.jobs(&blk)
@@ -67,6 +72,11 @@ module Joblin
67
72
  tracker.workflow_state = 'completed'
68
73
  tracker.save! if tracker.changed?
69
74
  end
75
+
76
+ def on_stagnated(status, options)
77
+ tracker = BackgroundTask.find(options['id'])
78
+ tracker.handle_batch_stagnation
79
+ end
70
80
  end
71
81
  end
72
82
  end
data/joblin.gemspec CHANGED
@@ -25,6 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'rediconn', '~> 0.1.0'
26
26
  spec.add_dependency 'rails', '>= 5', '< 9.0'
27
27
 
28
+ spec.add_dependency 'ostruct'
29
+
28
30
  spec.add_development_dependency 'rspec', '~> 3'
29
31
  spec.add_development_dependency 'redis'
30
32
  spec.add_development_dependency 'rspec-rails'
@@ -1,3 +1,3 @@
1
1
  module Joblin
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.1.4".freeze
3
3
  end