Module: DispatchPolicy

Defined in:
lib/dispatch_policy.rb,
lib/dispatch_policy/gate.rb,
lib/dispatch_policy/tick.rb,
lib/dispatch_policy/bypass.rb,
lib/dispatch_policy/config.rb,
lib/dispatch_policy/engine.rb,
lib/dispatch_policy/policy.rb,
lib/dispatch_policy/context.rb,
lib/dispatch_policy/railtie.rb,
lib/dispatch_policy/version.rb,
lib/dispatch_policy/decision.rb,
lib/dispatch_policy/pipeline.rb,
lib/dispatch_policy/registry.rb,
lib/dispatch_policy/forwarder.rb,
lib/dispatch_policy/tick_loop.rb,
lib/dispatch_policy/policy_dsl.rb,
lib/dispatch_policy/repository.rb,
lib/dispatch_policy/serializer.rb,
lib/dispatch_policy/job_extension.rb,
lib/dispatch_policy/gates/throttle.rb,
lib/dispatch_policy/operator_hints.rb,
app/models/dispatch_policy/partition.rb,
lib/dispatch_policy/inflight_tracker.rb,
app/models/dispatch_policy/staged_job.rb,
lib/dispatch_policy/cursor_pagination.rb,
lib/dispatch_policy/gates/concurrency.rb,
app/models/dispatch_policy/tick_sample.rb,
app/models/dispatch_policy/inflight_job.rb,
app/models/dispatch_policy/application_record.rb,
lib/dispatch_policy/gates/adaptive_concurrency.rb,
app/controllers/dispatch_policy/policies_controller.rb,
app/controllers/dispatch_policy/dashboard_controller.rb,
app/controllers/dispatch_policy/partitions_controller.rb,
app/models/dispatch_policy/adaptive_concurrency_stats.rb,
app/controllers/dispatch_policy/application_controller.rb,
app/controllers/dispatch_policy/staged_jobs_controller.rb,
lib/generators/dispatch_policy/install/install_generator.rb

Defined Under Namespace

Modules: Bypass, CursorPagination, Forwarder, Gates, Generators, InflightTracker, JobExtension, OperatorHints, Repository, Serializer, TickLoop Classes: AdaptiveConcurrencyStats, ApplicationController, ApplicationRecord, Config, Context, DashboardController, Decision, Engine, EnqueueFailed, Error, Gate, InflightJob, InvalidPolicy, Partition, PartitionsController, Pipeline, PoliciesController, Policy, PolicyAlreadyRegistered, PolicyDSL, Railtie, Registry, StagedJob, StagedJobsController, Tick, TickSample, UnknownGate

Constant Summary collapse

PG_BACKED_ADAPTER_HINTS =

Adapters whose enqueue runs against ActiveRecord::Base.connection (so the adapter INSERT can join the admission TX) or whose semantics make atomicity moot (test/inline). Substring match against the adapter class name keeps the check resilient to ActiveJob’s wrapper renames.

%w[GoodJob SolidQueue].freeze
EXEMPT_ADAPTER_HINTS =
%w[Test Inline Async].freeze
VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.configObject



50
51
52
# File 'lib/dispatch_policy.rb', line 50

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



46
47
48
# File 'lib/dispatch_policy.rb', line 46

def configure
  yield config
end

.registryObject



58
59
60
# File 'lib/dispatch_policy.rb', line 58

def registry
  @registry ||= Registry.new
end

.reset_config!Object



54
55
56
# File 'lib/dispatch_policy.rb', line 54

def reset_config!
  @config = Config.new
end

.reset_registry!Object



62
63
64
# File 'lib/dispatch_policy.rb', line 62

def reset_registry!
  @registry = Registry.new
end

.warn_unsupported_adapterObject

Logs a warning if the configured ActiveJob adapter is not one of the PG-backed ones the gem can guarantee atomic admission for. We do NOT raise: a host may use a custom PG-backed adapter we don’t recognize, or may have accepted the trade-off knowingly. The warning is enough to surface the issue at boot.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dispatch_policy.rb', line 71

def warn_unsupported_adapter
  return unless defined?(::ActiveJob::Base)
  adapter = ::ActiveJob::Base.queue_adapter
  return unless adapter

  klass_name = adapter.class.name.to_s
  return if (PG_BACKED_ADAPTER_HINTS + EXEMPT_ADAPTER_HINTS).any? { |hint| klass_name.include?(hint) }

  config.logger&.warn(
    "[dispatch_policy] active_job adapter is #{klass_name}; atomic admission requires " \
    "a PG-backed adapter that shares ActiveRecord::Base's connection (good_job, solid_queue). " \
    "If the worker process crashes between admission COMMIT and adapter enqueue, the job is lost. " \
    "Set DispatchPolicy.config.database_role if you use a separate DB role for queueing."
  )
end