class Tumugi::Workflow

Constants

DEFAULT_CONFIG_FILE

Attributes

id[R]
params[RW]

Public Class Methods

new() click to toggle source
# File lib/tumugi/workflow.rb, line 21
def initialize
  @id = SecureRandom.uuid
  @tasks = {}
  @params = {}
end

Public Instance Methods

add_task(id, task) click to toggle source
# File lib/tumugi/workflow.rb, line 38
def add_task(id, task)
  @tasks[id.to_s] = task
end
execute(command, root_task_id, options) click to toggle source
# File lib/tumugi/workflow.rb, line 27
def execute(command, root_task_id, options)
  @start_time = Time.now
  logger.info "start id: #{id}"
  process_common_options(command, options)
  load_workflow_file(options[:file])
  result = execute_command(command, root_task_id, options)
  @end_time = Time.now
  logger.info "end id: #{id}, elapsed_time: #{elapsed_time}"
  result
end
find_task(id) click to toggle source
# File lib/tumugi/workflow.rb, line 42
def find_task(id)
  task = @tasks[id.to_s]
  raise Tumugi::TumugiError, "Task not found: #{id}" if task.nil?
  task
end

Private Instance Methods

create_dag(id) click to toggle source
# File lib/tumugi/workflow.rb, line 124
def create_dag(id)
  dag = Tumugi::DAG.new
  task = find_task(id)
  dag.add_task(task)
  dag
end
elapsed_time() click to toggle source
# File lib/tumugi/workflow.rb, line 113
def elapsed_time
  human_readable_time((@end_time - @start_time).to_i)
end
execute_command(command, root_task_id, options) click to toggle source
# File lib/tumugi/workflow.rb, line 117
def execute_command(command, root_task_id, options)
  dag = create_dag(root_task_id)
  command_module = Kernel.const_get("Tumugi").const_get("Command")
  cmd = command_module.const_get("#{command.to_s.capitalize}").new
  cmd.execute(dag, options)
end
load_config(options) click to toggle source
# File lib/tumugi/workflow.rb, line 85
def load_config(options)
  config_file = options[:config]

  if config_file && !File.exist?(config_file)
    raise Tumugi::TumugiError, "Config file '#{config_file}' does not exist"
  end

  if !config_file && File.exist?(DEFAULT_CONFIG_FILE)
    config_file = DEFAULT_CONFIG_FILE
  end

  if config_file && File.exist?(config_file)
    logger.info "Load config from #{config_file}"
    begin
      load(config_file)
    rescue Exception => e
      raise Tumugi::TumugiError.new("Config file load error: #{config_file}", e)
    end
  end
end
load_workflow_file(file) click to toggle source
# File lib/tumugi/workflow.rb, line 50
def load_workflow_file(file)
  unless File.exist?(file)
    raise Tumugi::TumugiError, "Workflow file '#{file}' does not exist"
  end

  begin
    logger.info "Load workflow from #{file}"
    load(file, true)
  rescue Exception => e
    raise Tumugi::TumugiError.new("Workflow file load error: #{file}", e)
  end
end
logger() click to toggle source
# File lib/tumugi/workflow.rb, line 69
def logger
  @logger ||= Tumugi::ScopedLogger.new("tumugi-workflow")
end
process_common_options(command, options) click to toggle source
# File lib/tumugi/workflow.rb, line 63
def process_common_options(command, options)
  setup_logger(command, options)
  load_config(options)
  set_params(options)
end
set_params(options) click to toggle source
# File lib/tumugi/workflow.rb, line 106
def set_params(options)
  if options[:params]
    @params = options[:params]
    logger.info "Parameters: #{@params}"
  end
end
setup_logger(command, options) click to toggle source
# File lib/tumugi/workflow.rb, line 73
def setup_logger(command, options)
  log_format = (options[:log_format] || :text).to_sym
  if command == :run && !options[:out].nil?
    logger.init(output: options[:out], format: log_format)
  else
    logger.init(format: log_format)
  end
  logger.verbose! if options[:verbose]
  logger.quiet! if options[:quiet]
  logger.workflow_id = id
end