Class: Textus::Action::Enqueue
- Extended by:
- Contract::DSL
- Defined in:
- lib/textus/action/enqueue.rb
Instance Method Summary collapse
- #args ⇒ Object
- #call(container:, call:) ⇒ Object
-
#initialize(type:, args: {}) ⇒ Enqueue
constructor
A new instance of Enqueue.
Methods included from Contract::DSL
arg, around, cli, cli_stdin, contract, contract?, summary, surfaces, verb, view
Methods inherited from Base
Constructor Details
#initialize(type:, args: {}) ⇒ Enqueue
Returns a new instance of Enqueue.
17 18 19 20 21 |
# File 'lib/textus/action/enqueue.rb', line 17 def initialize(type:, args: {}) super() @type = type @job_args = args end |
Instance Method Details
#args ⇒ Object
23 24 25 |
# File 'lib/textus/action/enqueue.rb', line 23 def args { type: @type, args: @job_args } end |
#call(container:, call:) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/textus/action/enqueue.rb', line 27 def call(container:, call:) action_class = begin Textus::Jobs.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::JobStore::Job.new( type: @type, args: @job_args, enqueued_by: call.role, max_attempts: 3, ) Textus::Ports::JobStore.new(root: container.root).enqueue(job) { "protocol" => Textus::PROTOCOL, "ok" => true, "id" => job.id } end |