class Magellan::Gcs::Proxy::Cli

Constants

ACKSENDING
ACKSEND_ERROR
ACKSEND_OK
CLEANUP
DOWNLOADING
DOWNLOAD_ERROR
DOWNLOAD_OK
EXECUTE_ERROR
EXECUTE_OK
EXECUTING
PROCESSING
TOTAL
UPLOADING
UPLOAD_ERROR
UPLOAD_OK

Attributes

cmd_template[R]

Public Class Methods

new(*args) click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 19
def initialize(*args)
  @cmd_template = args.join(' ')
end

Public Instance Methods

build_command(context) click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 89
def build_command(context)
  msg_wrapper = MessageWrapper.new(context)
  r = ExpandVariable.expand_variables(cmd_template, msg_wrapper)
  if commands = Proxy.config[:commands]
    if template = commands[r]
      msg_wrapper = MessageWrapper.new(context)
      return ExpandVariable.expand_variables(template, msg_wrapper)
    else
      raise BuildError, "Invalid command key #{r.inspect} was given"
    end
  else
    return r
  end
end
build_command_with_error(context) click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 79
def build_command_with_error(context)
  return build_command(context)
rescue BuildError => e
  err = "[#{e.class.name}] #{e.message} with message: #{context.message.inspect}, the message will be acknowledged"
  context.notify(EXECUTE_ERROR, TOTAL, e.message)
  logger.error(err)
  context.message.acknowledge! # Send ACK not to process this message again
  return nil
end
process(msg) click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 58
def process(msg)
  context = Context.new(msg)
  context.notify(PROCESSING, TOTAL, "Processing message: #{msg.inspect}")
  context.setup do
    context.process_with_notification([DOWNLOADING, DOWNLOAD_OK, DOWNLOAD_ERROR], TOTAL, 'Download', &:download)

    cmd = build_command_with_error(context)
    return unless cmd

    exec = ->(*) { LoggerPipe.run(logger, cmd, returns: :none, logging: :both, dry_run: Proxy.config[:dryrun]) }
    context.process_with_notification([EXECUTING, EXECUTE_OK, EXECUTE_ERROR], TOTAL, 'Command', exec) do
      context.process_with_notification([UPLOADING, UPLOAD_OK, UPLOAD_ERROR], TOTAL, 'Upload', &:upload)

      context.process_with_notification([ACKSENDING, ACKSEND_OK, ACKSEND_ERROR], TOTAL, 'Acknowledge') do
        msg.acknowledge!
      end
    end
  end
  context.notify(CLEANUP, TOTAL, 'Cleanup')
end
process_with_error_handling(msg) click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 34
def process_with_error_handling(msg)
  process(msg)
rescue => e
  logger.error("[#{e.class.name}] #{e.message}")
  verbose("Backtrace\n  " << e.backtrace.join("\n  "))
end
run() click to toggle source
# File lib/magellan/gcs/proxy/cli.rb, line 23
def run
  logger.info("#{$PROGRAM_NAME}-#{VERSION} is running")
  logger.info('Start listening')
  GCP.subscription.listen do |msg|
    process_with_error_handling(msg)
  end
rescue => e
  logger.error("[#{e.class.name}] #{e.message}")
  raise e
end