#!/usr/bin/env ruby require 'optparse' require 'time' require 'iron_worker_ng' if $*.size == 0 || (not ['codes.create', 'tasks.create', 'schedules.create', 'tasks.log'].include?($*[0])) puts 'usage: iron_worker COMMAND [OPTIONS]' puts ' COMMAND: codes.create, tasks.create, schedules.create, tasks.log' puts ' run iron_worker COMMAND --help to get more information about each command' exit 1 end command = $*[0] $*.shift client = IronWorkerNG::Client.new if command == 'codes.create' 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 runtime = runtimes[0][:name] if runtime.nil? code = runtimes.find { |r| r[:name] == runtime }[:klass].new(:name => name) execute_features.each do |f| code.send(f[:name], *f[:args]) end client.codes.create(code) elsif command == 'tasks.create' || command == 'schedules.create' 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' 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('--live', 'live log session') 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