85 lines
2.1 KiB
Ruby
85 lines
2.1 KiB
Ruby
root = File.join(File.dirname(__FILE__), "..")
|
|
$LOAD_PATH.push root unless $LOAD_PATH.include? root
|
|
|
|
require "sidekiq"
|
|
require "sidekiq/api"
|
|
|
|
require "fileutils"
|
|
|
|
require "core/devops-config"
|
|
require "core/devops-logger"
|
|
require "core/devops-db"
|
|
require "providers/provider_factory"
|
|
require 'workers/workers_storage'
|
|
|
|
class Worker
|
|
include Sidekiq::Worker
|
|
|
|
module STATUS
|
|
INIT = "init"
|
|
RUNNING = "running"
|
|
COMPLETED = "completed"
|
|
FAILED = "failed"
|
|
IN_QUEUE = "queued"
|
|
end
|
|
|
|
def convert_config conf
|
|
config = {}
|
|
conf.each {|k,v| config[k.is_a?(String) ? k.to_sym : k] = v}
|
|
DevopsLogger.logger.debug "Config: #{config.inspect}"
|
|
config
|
|
end
|
|
|
|
def set_status id, status
|
|
Sidekiq.redis {|con| con.hset "devops", id, status}
|
|
end
|
|
|
|
# it is called from creating server handler. But maybe we can somehow refactore code
|
|
# to get rid of duplication?
|
|
# TODO: check it
|
|
def self.set_status id, status
|
|
Sidekiq.redis {|con| con.hset "devops", id, status}
|
|
end
|
|
|
|
def call conf, e_provider, dir
|
|
DevopsLogger.logger = logger
|
|
FileUtils.mkdir_p(dir) unless File.exists?(dir)
|
|
set_status jid, "init"
|
|
DevopsConfig.config = convert_config(conf)
|
|
file = File.join(dir, jid)
|
|
error = nil
|
|
provider = nil
|
|
begin
|
|
Devops::Db.init
|
|
unless e_provider.nil?
|
|
::Provider::ProviderFactory.init(config)
|
|
provider = ::Provider::ProviderFactory.get(e_provider)
|
|
end
|
|
rescue Exception => e
|
|
error = e
|
|
DevopsLogger.logger.error e.message
|
|
return
|
|
end
|
|
mongo = ::Devops::Db.connector
|
|
File.open(file, "w") do |out|
|
|
begin
|
|
set_status jid, STATUS::RUNNING
|
|
raise error unless error.nil?
|
|
status = yield(provider, out, file)
|
|
status = (status == 0 ? STATUS::COMPLETED : STATUS::FAILED)
|
|
set_status jid, status
|
|
mongo.set_report_status(jid, status)
|
|
status
|
|
rescue Exception => e
|
|
out << "\n"
|
|
out << e.message
|
|
out << "\n"
|
|
out << e.backtrace.join("\n")
|
|
set_status jid, STATUS::FAILED
|
|
mongo.set_report_status(jid, STATUS::FAILED) unless mongo.nil?
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|