class Fanforce::AppFactory::CLI::Iron include Fanforce::AppFactory::CLI::Utils require_gem 'iron_worker_ng', 'iron_worker_ng' attr_reader :app def initialize(app) @app = app end def upload(environment, worker_filename=nil) if !File.directory?("#{app.dir}/workers") return log "#{'Skipped '.format(:bold)} no workers folder was found" end env = Fanforce::AppFactory::CLI::Env.new(app) vars = env.app_vars(environment) || {} return puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} is missing IRON_TOKEN and/or IRON_PROJECT_ID env variables" if vars['IRON_TOKEN'].blank? or vars['IRON_PROJECT_ID'].blank? puts "#{'Updating Env'.format(:green,:bold)} #{environment.to_s.titleize}... and workers have #{vars.size} env variables" env.push_env_to(environment, vars, true) iron_worker = IronWorkerNG::Client.new(:token => vars['IRON_TOKEN'], :project_id => vars['IRON_PROJECT_ID']) Dir.chdir("#{app.dir}/workers") do workers = Dir['*.worker'] return puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} has 0 workers" if workers.size == 0 upload_processes = [] FileUtils.cp("#{app.dir}/workers/.env/#{environment}.rb", "#{app.dir}/workers/.env/.#{environment}env.rb") workers.each do |filename| next if worker_filename and !filename.include?(worker_filename) code_name = filename.gsub('.worker', '') puts "#{'Uploading'.format(:green,:bold)} #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..." code = IronWorkerNG::Code::Base.new(:workerfile => "#{app.dir}/workers/#{filename}") code.remote code.name = code_name code.file("#{app.dir}/workers/.env/.#{environment}env.rb") upload_processes << upload_iron_worker(iron_worker, code, filename, environment) end upload_processes.each { |pid| Process.waitpid(pid) } FileUtils.remove_file("#{app.dir}/workers/.env/.#{environment}env.rb") end end def upload_iron_worker(iron_worker, code, filename, environment) fork do begin iron_worker.codes.create(code, max_concurrency: 5) rescue Exception => e puts "#{'Aborted '.format(:red,:bold)} upload of #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..." puts e.message puts e.backtrace puts '' end end end end