Module: AcidicJob

Extended by:
ActiveSupport::Concern
Defined in:
lib/acidic_job/finished_point.rb,
lib/acidic_job.rb,
lib/acidic_job/run.rb,
lib/acidic_job/step.rb,
lib/acidic_job/errors.rb,
lib/acidic_job/staging.rb,
lib/acidic_job/testing.rb,
lib/acidic_job/version.rb,
lib/acidic_job/awaiting.rb,
lib/acidic_job/test_case.rb,
lib/acidic_job/recovery_point.rb,
lib/acidic_job/idempotency_key.rb,
lib/acidic_job/perform_wrapper.rb,
lib/acidic_job/upgrade_service.rb,
lib/acidic_job/extensions/noticed.rb,
lib/acidic_job/extensions/sidekiq.rb,
lib/acidic_job/extensions/active_job.rb,
lib/acidic_job/extensions/action_mailer.rb,
lib/generators/acidic_job/install_generator.rb,
lib/generators/acidic_job/drop_tables_generator.rb

Overview

Represents an action to set a new recovery point. One possible option for a return from an #atomic_phase block.

Defined Under Namespace

Modules: Awaiting, Extensions, Generators, PerformWrapper, Staging, Testing, UpgradeService Classes: Error, FinishedPoint, IdempotencyKey, Key, LockedIdempotencyKey, MismatchedIdempotencyKeyAndJobArguments, NoDefinedSteps, RecoveryPoint, Run, SerializedTransactionConflict, SidekiqBatchRequired, Staged, Step, TestCase, TooManyParametersForParallelJob, TooManyParametersForStepMethod, UnknownAtomicPhaseType, UnknownJobAdapter, UnknownRecoveryPoint, UnknownSerializedJobIdentifier

Constant Summary collapse

IDEMPOTENCY_KEY_LOCK_TIMEOUT =
90
VERSION =
"1.0.0.pre26"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wire_everything_up(klass) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/acidic_job.rb', line 28

def self.wire_everything_up(klass)
  # Ensure our `perform` method always runs first to gather parameters
  klass.prepend PerformWrapper

  klass.include Staging
  klass.include Awaiting

  # Add `deliver_acidicly` to ActionMailer
  ActionMailer::Parameterized::MessageDelivery.include Extensions::ActionMailer if defined?(ActionMailer)
  # Add `deliver_acidicly` to Noticed
  Noticed::Base.include Extensions::Noticed if defined?(Noticed)

  if defined?(ActiveJob) && klass < ActiveJob::Base
    klass.send(:include, Extensions::ActiveJob)
  elsif defined?(Sidekiq) && klass.include?(Sidekiq::Worker)
    klass.send(:include, Extensions::Sidekiq)
    klass.include ActiveSupport::Callbacks
    klass.define_callbacks :perform
  else
    raise UnknownJobAdapter
  end

  # TODO: write test for a staged job that uses awaits
  klass.set_callback :perform, :after, :reenqueue_awaited_by_job,
                     if: -> { was_awaited_job? && !was_workflow_job? }
  klass.set_callback :perform, :after, :finish_staged_job, if: -> { was_staged_job? && !was_workflow_job? }
  klass.define_callbacks :finish
  klass.set_callback :finish, :after, :reenqueue_awaited_by_job,
                     if: -> { was_workflow_job? && was_awaited_job? }

  klass.instance_variable_set(:@acidic_identifier, :job_id)
  klass.define_singleton_method(:acidic_by_job_id) { @acidic_identifier = :job_id }
  klass.define_singleton_method(:acidic_by_job_args) { @acidic_identifier = :job_args }
  klass.define_singleton_method(:acidic_by) { |proc| @acidic_identifier = proc }
  klass.attr_reader(:acidic_job_run)
end

Instance Method Details

#idempotency_keyObject

rubocop:disable Naming/MemoizedInstanceVariableName



127
128
129
130
131
132
133
134
135
# File 'lib/acidic_job.rb', line 127

def idempotency_key
  if defined?(@__acidic_job_idempotency_key) && !@__acidic_job_idempotency_key.nil?
    return @__acidic_job_idempotency_key
  end

  acidic_identifier = self.class.acidic_identifier
  @__acidic_job_idempotency_key ||= IdempotencyKey.new(acidic_identifier)
                                                  .value_for(self, *@__acidic_job_args, **@__acidic_job_kwargs)
end

#idempotently(with: {}, &blk) ⇒ Object

DEPRECATED



115
116
117
118
# File 'lib/acidic_job.rb', line 115

def idempotently(with: {}, &blk)
  ActiveSupport::Deprecation.new("1.0", "AcidicJob").deprecation_warning(:idempotently)
  with_acidity(providing: with, &blk)
end

#initialize(*args, **kwargs) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/acidic_job.rb', line 84

def initialize(*args, **kwargs)
  # ensure this instance variable is always defined
  @__acidic_job_steps = []
  @__acidic_job_args = args
  @__acidic_job_kwargs = kwargs

  super(*args, **kwargs)
rescue ArgumentError => e
  raise e unless e.message.include?("wrong number of arguments")

  super()
end

#safely_finish_acidic_jobObject



120
121
122
123
124
# File 'lib/acidic_job.rb', line 120

def safely_finish_acidic_job
  # Short circuits execution by sending execution right to 'finished'.
  # So, ends the job "successfully"
  FinishedPoint.new
end

#with_acidity(providing: {}) ⇒ Object

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/acidic_job.rb', line 97

def with_acidity(providing: {})
  # execute the block to gather the info on what steps are defined for this job workflow
  yield

  # check that the block actually defined at least one step
  # TODO: WRITE TESTS FOR FAULTY BLOCK VALUES
  raise NoDefinedSteps if @__acidic_job_steps.nil? || @__acidic_job_steps.empty?

  # convert the array of steps into a hash of recovery_points and next steps
  workflow = define_workflow(@__acidic_job_steps)

  @acidic_job_run = ensure_run_record(workflow, providing)

  # begin the workflow
  process_run(@acidic_job_run)
end