#!/usr/bin/env ruby require 'optparse' require 'time' require 'iron_worker_ng' if $*.size == 1 && ($*[0] == '-v' || $*[0] == '--version') puts IronWorkerNG.full_version exit 0 end if $*.size == 0 || (not ['codes.create', 'upload', 'tasks.create', 'queue', 'schedules.create', 'schedule', 'tasks.log', 'log'].include?($*[0])) puts 'usage: iron_worker COMMAND [OPTIONS]' puts ' COMMAND: codes.create (upload), tasks.create (queue), schedules.create (schedule), tasks.log (log)' puts ' run iron_worker COMMAND --help to get more information about each command' exit 1 end command = $*.shift command = 'codes.create' if command == 'upload' command = 'tasks.create' if command == 'queue' command = 'schedules.create' if command == 'schedule' command = 'tasks.log' if command == 'log' if level = ENV['LOG_LEVEL'] IronCore::Logger.logger.level = eval "::Logger::#{level}" end client = IronWorkerNG::Client.new if command == 'codes.create' if $*.size > 0 && $*[0][0] != '-' $*.unshift('-n') end runtimes = IronWorkerNG::Code::Base.registered_types runtimes_help = runtimes[0][:name] + ' (default)' + (runtimes.size == 1 ? '' : ', ') + runtimes.map { |r| r[:name] }[1 .. -1].join(', ') features = [] name = nil runtime = nil execute_features = [] opts = OptionParser.new do |opts| opts.banner = "usage: iron_worker #{command} [OPTIONS]" opts.on('-r', '--runtime RUNTIME', runtimes.map { |r| r[:name] }, "#{runtimes_help}") do |v| runtime = v end opts.on('-n', '--name NAME', 'code name') do |v| name = v end IronWorkerNG::Code::Base.registered_features.each do |f| prefix = '' if f[:for_klass] != IronWorkerNG::Code::Base prefix = runtimes.find { |r| r[:klass] == f[:for_klass] }[:name] + '-' end opts.on("--#{prefix}#{f[:name].gsub('_', '-')} #{f[:args]}", Array) do |v| execute_features << {:name => f[:name], :args => v} end end end begin opts.parse! rescue OptionParser::ParseError puts $!.to_s exit 1 end code = nil if runtime.nil? code = IronWorkerNG::Code::Creator.create(:name => name) else code = runtimes.find { |r| r[:name] == runtime }[:klass].new(:name => name) end execute_features.each do |f| code.send(f[:name], *f[:args]) end client.codes.create(code) elsif command == 'tasks.create' || command == 'schedules.create' if $*.size > 0 && $*[0][0] != '-' $*.unshift('-n') end name = nil payload = nil priority = nil timeout = nil delay = nil start_at = nil end_at = nil run_times = nil run_every = nil print_id = false opts = OptionParser.new do |opts| opts.banner = "usage: iron_worker #{command} [OPTIONS]" opts.on('-n', '--name NAME', 'code name') do |v| name = v end opts.on('-p', '--payload PAYLOAD', String, 'payload to pass') do |v| payload = v end opts.on('--priority PRIORITY', Integer, '0 (default), 1, 2') do |v| priority = v end opts.on('--timeout TIMEOUT', Integer, 'maximum run time in seconds from 0 to 3600 (default)') do |v| timeout = v end opts.on('--delay DELAY', Integer, 'delay before start in seconds') do |v| delay = v end if command == 'schedules.create' opts.on('--start-at TIME', 'start task at specified time') do |v| start_at = Time.parse(v) end opts.on('--end-at TIME', 'stop running task at specified time') do |v| end_at = Time.parse(v) end opts.on('--run-times RUN_TIMES', Integer, 'run task no more times than specified') do |v| run_times = v end opts.on('--run-every RUN_EVERY', Integer, 'run task every RUN_EVERY seconds') do |v| run_every = v end end opts.on('--print-id', 'prints result id') do |v| print_id = true end end begin opts.parse! rescue OptionParser::ParseError puts $!.to_s exit 1 end if name.nil? puts opts exit 1 end options = {} options[:priority] = priority unless priority.nil? options[:timeout] = timeout unless timeout.nil? options[:delay] = delay unless delay.nil? if command == 'schedules.create' options[:start_at] = start_at unless start_at.nil? options[:end_at] = end_at unless end_at.nil? options[:run_times] = run_times unless run_times.nil? options[:run_every] = run_every unless run_every.nil? end id = nil if command == 'tasks.create' id = client.tasks.create(name, payload, options).id else id = client.schedules.create(name, payload, options).id end print id if print_id elsif command == 'tasks.log' if $*.size > 0 && $*[0][0] != '-' $*.unshift('-t') end task_id = nil live = false opts = OptionParser.new do |opts| opts.banner = "usage: iron_worker #{command} [OPTIONS]" opts.on('-t', '--task-id ID', 'task id') do |v| task_id = v end opts.on('-w', '--wait', 'wait for task') do |v| live = true end end begin opts.parse! rescue OptionParser::ParseError puts $!.to_s exit 1 end if task_id.nil? puts opts exit 1 end log = '' if live begin log = client.tasks.log(task_id) rescue IronWorkerNG::APIClientError end else log = client.tasks.log(task_id) end print log if live client.tasks.wait_for(task_id) do |task| if task.status == 'running' begin next_log = client.tasks.log(task_id) print next_log[log.length .. - 1] log = next_log rescue IronWorkerNG::APIClientError end end end begin next_log = client.tasks.log(task_id) print next_log[log.length .. - 1] rescue IronWorkerNG::APIClientError end end end