#!/usr/bin/env ruby # # Copyright (c) 2011-2017 Cloudware S.A. All rights reserved. # # This file is part of sp-job. # # sp-job is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # sp-job is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with sp-job. If not, see . # # encoding: utf-8 # require 'bundler/setup' require 'sp/job' require 'sp/job' require 'redis' require 'backburner' require 'optparse' require 'sp/job/common' require 'syslog/logger' extend SP::Job::Common begin $args = {} $config = {} $args[:redis] = '127.0.0.1:6379' $args[:beanstalkd] = '127.0.0.1:11300' $args[:validity] = 7200 $args[:ttr] = 3600 # # Parse command line arguments # $option_parser = OptionParser.new do |opts| opts.banner = "Usage: #{$PROGRAM_NAME} ARGS" opts.on('-r', '--redis=HOST:PORT' , "Hostname and port of the redis server (default: '#{$args[:redis]}')") { |v| $args[:redis] = v } opts.on('-b', '--beanstalkd=HOST:PORT', "Hostname and port of the beanstalkd server (default: '#{$args[:beanstalkd]}')") { |v| $args[:beanstalkd] = v } opts.on('-V', '--validity=SECS' , "job validty in seconds") { |v| $args[:validity] = v } opts.on('-t', '--tube=TUBE' , "beanstalkd tube name") { |v| $args[:tube] = v } opts.on('-i', '--sid=SERVICEID' , "service id on redis") { |v| $config[:service_id] = v } opts.on('-v', '--ttr=SECS' , "job ttr time to run in seconds") { |v| $args[:ttr] = v } opts.on('-l', '--log=LOGFILE' , "path to log file (default: '#{$args[:log_file]}')") { |v| $args[:log_file] = File.expand_path(v) } opts.on('-d', '--debug' , "developer mode: log to stdout and print job") { $args[:debug] = true } end $option_parser.parse! raise "Tube must be specified with --tube!!!" if $args[:tube].nil? raise "Service id must be specified with --sid!!!" if $config[:service_id].nil? $redis = Redis.new(:host => $args[:redis].split(':')[0], :port => $args[:redis].split(':')[1], :db => 0) $beaneater = Beaneater.new $args[:beanstalkd] job = { } ARGV.each do |arg| key, value = arg.to_s.split('=') job[key.to_sym] = value end submit_job(job: job, tube: $args[:tube], ttr: $args[:ttr], validity: $args[:validity]) rescue => e STDERR.puts e STDERR.puts e.backtrace sys_log = Syslog::Logger.new $PROGRAM_NAME sys_log.error "#{e} #{e.backtrace}" end