Class: Pgbus::Recurring::CommandJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/pgbus/recurring/command_job.rb

Overview

Job class for command-based recurring tasks. Evaluates a method call chain on a constant, e.g. “OldRecord.cleanup!”.

Only supports ‘ConstantName.method_name` and `ConstantName.method_name(args)`. Rejects arbitrary Ruby expressions for safety.

Constant Summary collapse

SAFE_COMMAND =
/\A([A-Z][A-Za-z0-9_]*(?:::[A-Z][A-Za-z0-9_]*)*)\.([a-z_][a-z0-9_!?]*)\z/

Instance Method Summary collapse

Instance Method Details

#perform(command) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pgbus/recurring/command_job.rb', line 13

def perform(command)
  match = SAFE_COMMAND.match(command)
  unless match
    raise ArgumentError,
          "Unsafe recurring command: #{command.inspect}. " \
          "Must be in the form 'ClassName.method_name'."
  end

  klass = match[1].safe_constantize
  raise ArgumentError, "Unknown class in recurring command: #{match[1]}" unless klass

  klass.public_send(match[2])
end