class LittleMonster::Core::Job

Constants

CALLBACKS
ENDED_STATUS

Attributes

current_action[RW]
data[RW]
error[RW]
id[RW]
orchrestator[R]
retries[RW]
status[RW]
tags[RW]

Public Class Methods

callback_max_retries() click to toggle source
# File lib/little_monster/core/job.rb, line 31
def callback_max_retries
  @callback_max_retries ||= max_retries
end
callback_retries(value) click to toggle source
# File lib/little_monster/core/job.rb, line 17
def callback_retries(value)
  @callback_max_retries = value
end
max_retries() click to toggle source
# File lib/little_monster/core/job.rb, line 27
def max_retries
  @max_retries ||= LittleMonster.default_job_retries
end
mock!() click to toggle source
# File lib/little_monster/core/job.rb, line 35
def mock!
  @mock = true
end
mock?() click to toggle source
# File lib/little_monster/core/job.rb, line 43
def mock?
  @mock ||= false
end
new(options = {}) click to toggle source
# File lib/little_monster/core/job.rb, line 59
def initialize(options = {})
  @id = options.fetch(:id, nil)
  @tags = (options[:tags] || {}).freeze

  @retries = options[:retries] || 0

  @current_action = options.fetch(:current_action, self.class.tasks.first)

  @data = if options[:data]
            Data.new(self, options[:data])
          else
            Data.new(self)
          end

  @status = options.fetch(:status, :pending)
  @error= options.fetch(:error, {})

  @orchrestator = Job::Orchrestator.new(self)

  if mock?
    @runned_tasks = {}
    self.class.send :attr_reader, :runned_tasks
  end

  logger.default_tags = tags.merge(
    id: @id,
    job: self.class.to_s,
    retry: @retries
  )

  logger.info "[type:start_job] Starting job with data: #{data.to_h[:outputs]}"
end
retries(value) click to toggle source
# File lib/little_monster/core/job.rb, line 13
def retries(value)
  @max_retries = value
end
task_class_for(task_name) click to toggle source
# File lib/little_monster/core/job.rb, line 21
def task_class_for(task_name)
  "#{to_s.underscore}/#{task_name}".camelcase.constantize
rescue NameError
  task_name.to_s.camelcase.constantize
end
task_list(*tasks) click to toggle source
# File lib/little_monster/core/job.rb, line 9
def task_list(*tasks)
  @tasks = *tasks
end
tasks() click to toggle source
# File lib/little_monster/core/job.rb, line 39
def tasks
  @tasks ||= []
end

Public Instance Methods

callback_running?() click to toggle source
# File lib/little_monster/core/job.rb, line 183
def callback_running?
  return false if @current_action.nil? || self.class.tasks.include?(@current_action)
  CALLBACKS.include? @current_action
end
callback_to_run() click to toggle source
# File lib/little_monster/core/job.rb, line 163
def callback_to_run
  case @status
  when :success
    :on_success
  when :error
    :on_error
  when :cancelled
    :on_cancel
  end
end
ended_status?() click to toggle source
# File lib/little_monster/core/job.rb, line 188
def ended_status?
  Job::ENDED_STATUS.include? @status
end
is_cancelled?() click to toggle source
# File lib/little_monster/core/job.rb, line 140
def is_cancelled?
  return false unless should_request?
  resp = LittleMonster::API.get "/jobs/#{id}"

  if resp.success?
    resp.body[:cancel]
  else
    false
  end
end
max_retries() click to toggle source
# File lib/little_monster/core/job.rb, line 155
def max_retries
  callback_running? ? self.class.callback_max_retries : self.class.max_retries
end
mock?() click to toggle source
# File lib/little_monster/core/job.rb, line 192
def mock?
  self.class.mock?
end
notify_callback(status, options = {}) click to toggle source
# File lib/little_monster/core/job.rb, line 116
def notify_callback(status, options = {})
  return true unless should_request?
  params = { body: { name: @current_action, status: status } }

  params[:body][:exception] = serialize_error(options[:exception]) if options[:exception]

  params[:body].merge!(options.except(:exception))

  resp = LittleMonster::API.put "/jobs/#{id}/callbacks/#{@current_action}", params,
                                retries: LittleMonster.task_requests_retries,
                                retry_wait: LittleMonster.task_requests_retry_wait
  resp.success?
end
notify_job(params = {}, options = {}) click to toggle source
# File lib/little_monster/core/job.rb, line 130
def notify_job(params = {}, options = {})
  return true unless should_request?
  options[:critical] = true

  params[:body][:data] = params[:body][:data].to_h if params[:body][:data]

  resp = LittleMonster::API.put "/jobs/#{id}", params, options
  resp.success?
end
notify_status(options = {}) click to toggle source
# File lib/little_monster/core/job.rb, line 96
def notify_status(options = {})
  params = { body: { status: @status } }
  params[:body].merge!(options)

  notify_job params, retries: LittleMonster.job_requests_retries,
                     retry_wait: LittleMonster.job_requests_retry_wait
end
notify_task(status, options = {}) click to toggle source
# File lib/little_monster/core/job.rb, line 104
def notify_task(status, options = {})
  params = { body: { tasks: [{ name: @current_action, status: status }] } }

  params[:body][:data] = options[:data] if options[:data]
  params[:body][:tasks].first[:exception] = serialize_error(options[:exception]) if options[:exception]

  params[:body][:tasks].first.merge!(options.except(:data, :exception))

  notify_job params, retries: LittleMonster.task_requests_retries,
                     retry_wait: LittleMonster.task_requests_retry_wait
end
on_cancel() click to toggle source
# File lib/little_monster/core/job.rb, line 211
def on_cancel ; end
on_error() click to toggle source

callbacks definition

# File lib/little_monster/core/job.rb, line 209
def on_error ; end
on_success() click to toggle source
# File lib/little_monster/core/job.rb, line 210
def on_success ; end
retry?() click to toggle source
# File lib/little_monster/core/job.rb, line 159
def retry?
  !mock? && (max_retries == -1 || max_retries > @retries)
end
run() click to toggle source
# File lib/little_monster/core/job.rb, line 92
def run
  @orchrestator.run
end
serialize_error(error) click to toggle source
# File lib/little_monster/core/job.rb, line 200
def serialize_error(error)
  {
    message: error.message,
    type: error.class.to_s,
    retry: @retries
  }
end
should_request?() click to toggle source
# File lib/little_monster/core/job.rb, line 196
def should_request?
  !(mock? || LittleMonster.disable_requests?)
end
task_class_for(task_name) click to toggle source
# File lib/little_monster/core/job.rb, line 151
def task_class_for(task_name)
  self.class.task_class_for task_name
end
tasks_to_run() click to toggle source

returns the tasks that will be runned for this instance

# File lib/little_monster/core/job.rb, line 175
def tasks_to_run
  return [] if callback_running?
  task_index = self.class.tasks.find_index(@current_action)

  return [] if task_index.nil?
  self.class.tasks.slice(task_index..-1)
end