acidic_job 0.8.1 → 0.8.4

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: '0924564388ca928e9923161a95788dfb393e71f48d82ab6474e221793befc6af'
4
- data.tar.gz: 0a06d0e7f933a9e0e319bc698a3ab216502033b9c9df46fa7646cd77bdb1109c
3
+ metadata.gz: 041e2488af3dc4780e6112b92195dc805063ebc4c631bd3d7012017efe4e1583
4
+ data.tar.gz: d035a4d739e3cc7b6f6a2f223aefa7d600ab67dae537814363f16a478dd8c00c
5
5
  SHA512:
6
- metadata.gz: db1b579a5887707c41534d825d261a024a25cab2c5619af1179d41360af85e8271bf16dc9fb29442aa1442f9d49366dbec60a8d8d3ea010eb15d4a413238fe40
7
- data.tar.gz: 1c371720712b4eb34aec5c8b2aba077906100cc0b69bd72624844260c01522dfa4c5d843fea21b08efdc199dedfd08e8966c602b12bd73e04c8615c4e00f7487
6
+ metadata.gz: 96792f3e03f71aa19a83773a3eded77de541e6ea72c9aa2ca421f5298aa5e0e234b0705b0a27f417ebdb02b8e0b621ab935571b5ffc728a729c22bff7f97d809
7
+ data.tar.gz: 7dc53950ebf1d1c425f02f2c12a065cefa71c8c57173a4aa27478d9fcab7ffc0cdc822034da9179d59da9a0c544bd8b7c81108ef4da99a941de4b1d9ec63eb28
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acidic_job (0.8.1)
4
+ acidic_job (0.8.4)
5
5
  activejob
6
6
  activerecord
7
7
  activesupport
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # AcidicJob
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/acidic_job.svg)](https://badge.fury.io/rb/acidic_job)
4
- ![main workflow](https://github.com/fractaledmind/acidic_job/actions/workflows/main.yml/badge.svg)
3
+ [![Gem Version](https://badge.fury.io/rb/acidic_job.svg)](https://rubygems.org/gems/acidic_job)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/acidic_job)](https://rubygems.org/gems/acidic_job)
5
+ ![Tests](https://github.com/fractaledmind/acidic_job/actions/workflows/main.yml/badge.svg)
6
+ ![Coverage](https://img.shields.io/badge/code%20coverage-98%25-success)
7
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/e0df63f7a6f141d4aecc3c477314fdb2)](https://www.codacy.com/gh/fractaledmind/acidic_job/dashboard?utm_source=github.com&utm_medium=referral&utm_content=fractaledmind/acidic_job&utm_campaign=Badge_Grade)
5
8
 
6
9
  ## Idempotent operations for Rails apps (for ActiveJob or Sidekiq)
7
10
 
@@ -306,24 +309,22 @@ class ExampleJob < AcidicJob::Base
306
309
  end
307
310
  ```
308
311
 
309
- These options cover the two common situations, but sometimes our systems need finer-grained control. For example, our job might take some record as the job argument, but we need to use a combination of the record identifier and record status as the foundation for the idempotency key. In these cases you can pass a `Proc` to an `acidic_by` class method:
312
+ These options cover the two common situations, but sometimes our systems need finer-grained control. For example, our job might take some record as the job argument, but we need to use a combination of the record identifier and record status as the foundation for the idempotency key. In these cases you can pass a `Proc` or a `Block` to an `acidic_by` class method. This code will be executed in the context of the newly initialized job instance, so you will have access to whatever data the job is initialized with (like the `arguments`, for example):
310
313
 
311
314
  ```ruby
312
315
  class ExampleJob < AcidicJob::Base
313
- acidic_by -> { [@record.id, @record.status] }
316
+ acidic_by do
317
+ record = arguments.first[:record]
318
+ [record.id, record.status]
319
+ end
314
320
 
315
321
  def perform(record:)
316
- @record = record
317
-
318
- # the idempotency key will be based on whatever the values of `@record.id` and `@record.status` are
319
- with_acidic_workflow do |workflow|
320
- workflow.step :do_something
321
- end
322
+ # ...
322
323
  end
323
324
  end
324
325
  ```
325
326
 
326
- > **Note:** The `acidic_by` proc _will be executed in the context of the job instance_ at the moment the `with_acidic_workflow` method is called. This means it will have access to any instance variables defined in your `perform` method up to that point.
327
+ > **Note:** The `acidic_by` proc/block _will be executed in the context of the job instance_ at the moment the job is initialized. This means it will **not** have access to any instance variables defined in your `perform` method.
327
328
 
328
329
 
329
330
  ### Sidekiq Callbacks
@@ -18,7 +18,7 @@ module AcidicJob
18
18
  # You could unique job runs by the arguments passed to the job (e.g. memoization)
19
19
  other.define_singleton_method(:acidic_by_job_arguments) { @acidic_identifier = :job_arguments }
20
20
  # Or, you could unique jobs run by any logic you'd like using a block
21
- other.define_singleton_method(:acidic_by) { |&block| @acidic_identifier = block }
21
+ other.define_singleton_method(:acidic_by) { |proc = nil, &block| @acidic_identifier = proc || block }
22
22
 
23
23
  # We add a callback to ensure that staged, non-workflow jobs are "finished" after they are "performed".
24
24
  # This allows us to ensure that we can always inspect whether a run is finished and get correct data
@@ -77,8 +77,7 @@ module AcidicJob
77
77
  end
78
78
 
79
79
  def with_acidic_workflow(persisting: {}, &block)
80
- raise UnknownJobAdapter unless (defined?(::AcidicJob::Base) && self.class < ::AcidicJob::Base) ||
81
- (defined?(::AcidicJob::ActiveKiq) && self.class < ::AcidicJob::ActiveKiq)
80
+ raise UnknownJobAdapter unless known_job_adapter?
82
81
 
83
82
  raise RedefiningWorkflow if defined? @workflow_builder
84
83
 
@@ -250,5 +249,13 @@ module AcidicJob
250
249
  :serializable
251
250
  end
252
251
  end
252
+
253
+ def known_job_adapter?
254
+ return true if defined?(::AcidicJob::Base) && self.class < ::AcidicJob::Base
255
+ return true if defined?(::AcidicJob::ActiveKiq) && self.class < ::AcidicJob::ActiveKiq
256
+ return true if defined?(::ActiveJob) && self.class < ::ActiveJob::Base
257
+
258
+ false
259
+ end
253
260
  end
254
261
  end
@@ -8,9 +8,9 @@ module AcidicJob
8
8
  @arguments = args
9
9
 
10
10
  # we don't want to run the `perform` callbacks twice, since ActiveJob already handles that for us
11
- if defined?(ActiveJob) && self.class < ActiveJob::Base
11
+ if defined?(::ActiveJob) && self.class < ::ActiveJob::Base
12
12
  super(*args)
13
- elsif defined?(Sidekiq) && self.class.include?(Sidekiq::Worker)
13
+ elsif defined?(::Sidekiq) && self.class.include?(::Sidekiq::Worker)
14
14
  run_callbacks :perform do
15
15
  super(*args)
16
16
  end
@@ -20,14 +20,17 @@ module AcidicJob
20
20
  Serializers::JobSerializer,
21
21
  Serializers::RangeSerializer,
22
22
  Serializers::RecoveryPointSerializer,
23
- Serializers::WorkerSerializer
23
+ Serializers::WorkerSerializer,
24
+ Serializers::ActiveKiqSerializer
24
25
  )
25
26
  end
26
27
  end
27
28
 
29
+ # :nocov:
28
30
  generators do
29
31
  require "generators/acidic_job/install_generator"
30
32
  end
33
+ # :nocov:
31
34
 
32
35
  # This hook happens after all initializers are run, just before returning
33
36
  config.after_initialize do
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_job/serializers/object_serializer"
4
+
5
+ module AcidicJob
6
+ module Serializers
7
+ class ActiveKiqSerializer < ::ActiveJob::Serializers::ObjectSerializer
8
+ def serialize(worker)
9
+ super(
10
+ "job_class" => worker.class.name,
11
+ "arguments" => Arguments.serialize(worker.arguments),
12
+ )
13
+ end
14
+
15
+ def deserialize(hash)
16
+ worker_class = hash["job_class"].constantize
17
+ worker_class.new(*hash["arguments"])
18
+ end
19
+
20
+ def serialize?(argument)
21
+ defined?(::AcidicJob::ActiveKiq) && argument.class < ::AcidicJob::ActiveKiq
22
+ end
23
+ end
24
+ end
25
+ end
@@ -13,7 +13,7 @@ module AcidicJob
13
13
  "backtrace" => {}
14
14
  }
15
15
 
16
- exception.backtrace.map do |trace|
16
+ exception&.backtrace&.map do |trace|
17
17
  path, _, location = trace.rpartition("/")
18
18
 
19
19
  next if hash["backtrace"].key?(path)
@@ -10,7 +10,7 @@ module AcidicJob
10
10
  # by comparing the deserialized database value with a temporary in-memory generated value.
11
11
  # That temporary in-memory generated value can sometimes have an `enqueued_at` value that is 1 second off
12
12
  # from the original. In this case, ActiveRecord will think the record has unsaved changes and block the lock.
13
- super(job.as_json.merge("job_class" => job.class.name))
13
+ super(job.serialize.except("enqueued_at"))
14
14
  end
15
15
 
16
16
  def deserialize(hash)
@@ -2,26 +2,24 @@
2
2
 
3
3
  require "active_job/serializers/object_serializer"
4
4
 
5
- # :nocov:
6
5
  module AcidicJob
7
6
  module Serializers
8
7
  class WorkerSerializer < ::ActiveJob::Serializers::ObjectSerializer
9
8
  def serialize(worker)
10
9
  super(
11
- "job_class" => worker.class.name,
12
- "arguments" => worker.arguments,
10
+ "job_class" => worker.class.name
13
11
  )
14
12
  end
15
13
 
16
14
  def deserialize(hash)
17
15
  worker_class = hash["job_class"].constantize
18
- worker_class.new(*hash["arguments"])
16
+ worker_class.new
19
17
  end
20
18
 
21
19
  def serialize?(argument)
22
- defined?(::Sidekiq) && argument.class.include?(::Sidekiq::Worker)
20
+ defined?(::Sidekiq) && argument.class.include?(::Sidekiq::Worker) &&
21
+ !(defined?(::AcidicJob::ActiveKiq) && argument.class < ::AcidicJob::ActiveKiq)
23
22
  end
24
23
  end
25
24
  end
26
25
  end
27
- # :nocov:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AcidicJob
4
- VERSION = "0.8.1"
4
+ VERSION = "0.8.4"
5
5
  end
data/lib/acidic_job.rb CHANGED
@@ -27,6 +27,7 @@ require_relative "acidic_job/serializers/job_serializer"
27
27
  require_relative "acidic_job/serializers/range_serializer"
28
28
  require_relative "acidic_job/serializers/recovery_point_serializer"
29
29
  require_relative "acidic_job/serializers/worker_serializer"
30
+ require_relative "acidic_job/serializers/active_kiq_serializer"
30
31
 
31
32
  require_relative "acidic_job/rails"
32
33
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acidic_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - fractaledmind
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-17 00:00:00.000000000 Z
11
+ date: 2022-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -266,6 +266,7 @@ files:
266
266
  - lib/acidic_job/recovery_point.rb
267
267
  - lib/acidic_job/run.rb
268
268
  - lib/acidic_job/serializer.rb
269
+ - lib/acidic_job/serializers/active_kiq_serializer.rb
269
270
  - lib/acidic_job/serializers/exception_serializer.rb
270
271
  - lib/acidic_job/serializers/finished_point_serializer.rb
271
272
  - lib/acidic_job/serializers/job_serializer.rb