Class: Textus::Action::Enqueue

Inherits:
WriteVerb show all
Extended by:
Contract::DSL
Defined in:
lib/textus/action/enqueue.rb

Constant Summary collapse

BURN =
:sync

Instance Method Summary collapse

Methods included from Contract::DSL

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

Methods inherited from Base

inherited

Constructor Details

#initialize(type:, args: {}) ⇒ Enqueue

Returns a new instance of Enqueue.



19
20
21
22
23
# File 'lib/textus/action/enqueue.rb', line 19

def initialize(type:, args: {})
  super()
  @type = type
  @job_args = args
end

Instance Method Details

#argsObject



25
26
27
# File 'lib/textus/action/enqueue.rb', line 25

def args
  { type: @type, args: @job_args }
end

#call(container:, call:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/textus/action/enqueue.rb', line 29

def call(container:, call:)
  action_class = begin
    Textus::Background::Job.fetch(@type.to_s)
  rescue Textus::UsageError
    raise Textus::UsageError.new("unregistered job type '#{@type}'")
  end
  if action_class.const_defined?(:REQUIRED_ROLE) && call.role != action_class::REQUIRED_ROLE
    raise Textus::Error.new(
      "forbidden",
      "role '#{call.role}' is not authorized to enqueue this job type (requires '#{action_class::REQUIRED_ROLE}')",
      details: { "role" => call.role, "required_role" => action_class::REQUIRED_ROLE },
      exit_code: 77,
    )
  end

  job = Textus::Ports::Queue::Job.new(
    type: @type,
    args: @job_args,
    enqueued_by: call.role,
    max_attempts: 3,
  )
  Textus::Ports::Queue.new(root: container.root).enqueue(job)
  { "protocol" => Textus::PROTOCOL, "ok" => true, "id" => job.id }
end