Module: Kommando::CommandPlugins::Schedule::ClassMethods

Defined in:
lib/kommando/command_plugins/schedule.rb

Instance Method Summary collapse

Instance Method Details

#_after_execute(context) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/kommando/command_plugins/schedule.rb', line 58

def _after_execute(context)
  results = context[:instance].scheduled_command_results

  if failed_result = results.find(&:error?)
    super(context.merge(halt: CommandResult.failure({ command: name, parameters: context[:parameters] }.merge({ error: :scheduling_error, details: failed_result.error }))))
  else
    super(context)
  end
end

#_after_schedule(context) ⇒ Object



49
50
51
# File 'lib/kommando/command_plugins/schedule.rb', line 49

def _after_schedule(context)
  context
end

#_before_execute(context) ⇒ Object



53
54
55
56
# File 'lib/kommando/command_plugins/schedule.rb', line 53

def _before_execute(context)
  context[:instance].scheduled_command_results = []
  super(context)
end

#_before_schedule(context) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/kommando/command_plugins/schedule.rb', line 37

def _before_schedule(context)
  unless context[:parameters].key?(:command_id)
    context[:parameters] = context[:parameters].merge({ command_id: SecureRandom.uuid })
  end

  context
end

#_execute_context_to_data(data, context) ⇒ Object



72
73
74
75
# File 'lib/kommando/command_plugins/schedule.rb', line 72

def _execute_context_to_data(data, context)
  results = context[:instance].scheduled_command_results
  super(data.merge(scheduled_commands: results.map(&:value)), context)
end

#_halt_schedule_with_failure(context) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/kommando/command_plugins/schedule.rb', line 81

def _halt_schedule_with_failure(context)
  case context[:halt]
  when CommandResult::Failure
    context[:halt]
  else
    CommandResult.failure(_schedule_context_to_data({}, context).merge({ error: context[:halt][:error], details: context[:halt][:data] }))
  end
end

#_schedule(context) ⇒ Object



45
46
47
# File 'lib/kommando/command_plugins/schedule.rb', line 45

def _schedule(context)
  context.merge(schedule_return_value: context[:schedule_adapter].schedule!(name, context[:parameters], context[:handle_at]))
end

#_schedule_context_to_data(data, context) ⇒ Object



77
78
79
# File 'lib/kommando/command_plugins/schedule.rb', line 77

def _schedule_context_to_data(data, context)
  data.merge(context.slice(:parameters)).merge(command: name)
end

#_schedule_context_to_result(context) ⇒ Object



68
69
70
# File 'lib/kommando/command_plugins/schedule.rb', line 68

def _schedule_context_to_result(context)
  CommandResult.success(_schedule_context_to_data({}, context))
end

#schedule(dependencies, parameters) ⇒ Object



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
# File 'lib/kommando/command_plugins/schedule.rb', line 10

def schedule(dependencies, parameters)
  unless dependencies.key?(:schedule_adapter)
    raise Command::MissingDependencyError, 'You need to provide the `:schedule_adapter` dependency'
  end

  unless parameters.key?(:handle_at)
    raise Command::MissingParameterError, 'You need to provide the `:handle_at` parameter'
  end

  context = {
    parameters: parameters,
    schedule_adapter: dependencies[:schedule_adapter],
    handle_at: parameters[:handle_at].getutc,
  }

  context = _before_schedule(context)
  return _halt_schedule_with_failure(context) if context.key?(:halt)

  context = _schedule(context)
  return _halt_schedule_with_failure(context) if context.key?(:halt)

  context = _after_schedule(context)
  return _halt_schedule_with_failure(context) if context.key?(:halt)

  _schedule_context_to_result(context)
end