Class: Legion::Cli::Trigger

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/trigger.rb

Instance Method Summary collapse

Instance Method Details

#queue(*args) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legion/cli/trigger.rb', line 9

def queue(*args) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
  include Legion::Extensions::Helpers::Task
  response = if options['extension'].is_a? String
               options[:extension]
             else
               ask 'trigger extension?', limited_to: Legion::Data::Model::Extension.map(:name)
             end
  trigger_extension = Legion::Data::Model::Extension.where(name: response).first
  runners = Legion::Data::Model::Runner.where(extension_id: trigger_extension.values[:id])
  if runners.count == 1
    trigger_runner = runners.first
    say "Auto selecting #{trigger_runner.values[:name]} since it is the only option for runners"
  else
    response = options[:runner].is_a?(String) ? options[:runner] : ask('trigger runner?', limited_to: runners.map(:name))
    trigger_runner = Legion::Data::Model::Runner.where(name: response).where(extension_id: trigger_extension.values[:id]).first
  end

  functions = Legion::Data::Model::Function.where(runner_id: trigger_runner.values[:id])

  if functions.count == 1
    trigger_function = functions.first
    say "Auto selecting #{trigger_function.values[:name]} since it is the only option for functions"
  else
    response = if options[:function].is_a?(String)
                 options[:function]
               else
                 ask('trigger function?',
                     limited_to: Legion::Data::Model::Function.where(runner_id: trigger_runner.values[:id]).map(:name))
               end
    trigger_function = Legion::Data::Model::Function.where(runner_id: trigger_runner.values[:id]).where(name: response).first
  end
  say "#{trigger_runner.values[:namespace]}.#{trigger_function.values[:name]} selected as trigger", :green, :italicized
  payload = {}
  auto_opts = {}
  unless args.count.zero?
    args.each do |arg|
      test = arg.split(':')
      auto_opts[test[0].to_sym] = test[1]
    end
  end

  Legion::JSON.load(trigger_function.values[:args]).each do |arg, required|
    next if %w[args payload opts options].include? arg.to_s

    if auto_opts.key? arg
      payload[arg.to_sym] = auto_opts[arg]
      next
    end
    response = ask "#{required == 'keyreq' ? '[required]' : '[optional]'} #{arg} value:"
    if response.empty? && required == 'keyreq'
      say "Error! #{arg} is required and cannot be empty", :red
      redo
    end
    payload[arg.to_sym] = response unless response.empty?
  end

  status = options[:delay].zero? ? 'task.queued' : 'task.delayed'
  task = generate_task_id(function_id: trigger_function.values[:id], status: status, runner_id: trigger_runner.values[:id], args: payload,
                          delay: options[:delay])

  unless options[:delay].zero?
    say "Task: #{task[:task_id]} is queued and will be run in #{options[:delay]}s"
    return true
  end

  routing_key = "#{trigger_extension.values[:exchange]}.#{trigger_runner.values[:queue]}.#{trigger_function.values[:name]}"
  exchange = Legion::Transport::Messages::Dynamic.new(function: trigger_function.values[:name], function_id: trigger_function.values[:id],
                                                      routing_key: routing_key, args: payload)
  exchange.options[:task_id] = task[:task_id]
  exchange.publish if options[:delay].zero?

  say "Task: #{task[:task_id]} was queued"
end