Class: Textus::Action::Enqueue

Inherits:
Base
  • Object
show all
Defined in:
lib/textus/action/enqueue.rb

Class Method Summary collapse

Methods inherited from Base

inherited, proposal_from

Methods included from Contract::DSL

#arg, #cli, #cli_stdin, #contract, #contract?, #summary, #surfaces, #verb, #view

Class Method Details

.call(container:, call:, type:, args: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/textus/action/enqueue.rb', line 15

def self.call(container:, call:, type:, args: {})
  action_class = Textus::Jobs.fetch(type.to_s)

  if action_class.const_defined?(:REQUIRED_ROLE) && call.role != action_class::REQUIRED_ROLE
    return Failure(code: :forbidden,
                   message: "role '#{call.role}' is not authorized to enqueue this job type",
                   details: { "role" => call.role, "required_role" => action_class::REQUIRED_ROLE })
  end

  job = Textus::Store::Jobs::Queue::Job.new(
    type: type,
    args: args,
    role: call.role,
    max_attempts: 3,
  )
  Textus::Store::Jobs::Queue.new(store: container.job_store).enqueue(job)
  Success({ "protocol" => Textus::PROTOCOL, "ok" => true, "id" => job.id })
rescue Textus::UsageError
  Failure(code: :usage_error, message: "unregistered job type '#{type}'")
end