Sha256: 11069bac77f658c53a2526d42abb26b80be9fc0d4bc424a383e926450edbe29b

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

#!/usr/bin/env ruby

require 'date'

require 'terminal-table'

require "rawbotz"
require 'rawbotz/option_parser'
require 'rawbotz/cli/order_result_table'

optparse = Rawbotz::OptionParser.new do |opts, options|
  opts.banner = "Usage: #{$PROGRAM_NAME} [OPTIONS] ORDER_ID"
  opts.separator ""

  opts.on("-m", "--[no-]mail", "Send mail with result") do |m|
    options[:mail] = m
  end
end
optparse.parse!
options = optparse.options

include RawgentoModels

STDOUT.sync = true # helps in shell redirection logging situations

def main options
  logger = Logger.new(STDOUT)
  logger.level = options[:verbose] ? Logger::DEBUG : Logger::INFO

  logger.debug "#{$PROGRAM_NAME} #{Rawbotz::VERSION}"

  logger.info("Checking queue")

  queue = RawgentoModels::Order.queued
  logger.info("-> #{queue.count} orders to process")

  if queue.count == 0
    logger.info("Nothing to do, exiting.")
    exit 0
  end

  order = queue.first

  logger.info("#{order.order_items.length} items in Order")

  if order.state != "queued"
    logger.warn("Order in state #{order.state}!")
  end
  order.update(state: :processing)

  logger.info ("#{order.order_items.processible.count} products to order.")

  begin
    order_processor = Rawbotz::OrderProcessor.new(order, logger)
    order_processor.process!
    order.update(state: :ordered)
  rescue
    logger.error("Hit error during order")
    order.update(state: :error)
  end

  logger.info("Finished order")

  logger.info("Checking current cart")
  diffs = order_processor.check_against_cart

  # Option to put to stdout
  diff_table = Rawbotz::CLI::OrderResultTable.tables diffs
  puts diff_table

  order.update(order_result: diff_table)

  if options[:mail]
    logger.debug("Sending mail")
    mail = Rawbotz::mail("rawbotz order finished",
                  "Finished Order nr #{order.id}. Result: [tbd]\n\n#{diff_table}")
    logger.debug("Mail sent: #{mail}")
  else
    logger.debug("not sending mail")
  end
end

main options
exit 0

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rawbotz-0.2.0 exe/rawbotz_process_order_queue