rosa-build/lib/abf_worker/model_helper.rb

125 lines
2.7 KiB
Ruby
Raw Normal View History

module AbfWorker::ModelHelper
# In model which contains this helper should be:
# - #abf_worker_args
# - #build_canceled
MASS_BUILDS_SET = 'abf-worker::mass-builds'
USER_BUILDS_SET = 'abf-worker::user-builds'
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def log_server
@log_server ||= Redis.new(
2014-01-21 04:51:49 +00:00
host: APP_CONFIG['abf_worker']['log_server']['host'],
port: APP_CONFIG['abf_worker']['log_server']['port']
)
end
def self.next_build
raise NotImplementedError
end
end
def abf_worker_log
self.class.log_server.get(service_queue) || I18n.t('layout.build_lists.log.not_available')
end
def add_job_to_abf_worker_queue
update_build_sets
Resque.push(
worker_queue_with_priority,
'class' => worker_queue_class,
'args' => [abf_worker_args]
)
end
2012-12-28 14:00:37 +00:00
2013-11-29 12:58:37 +00:00
def restart_job
update_build_sets
Resque.redis.lpush "queue:#{worker_queue_with_priority}",
2013-11-29 12:58:37 +00:00
Resque.encode({'class' => worker_queue_class, 'args' => [abf_worker_args]})
end
def cancel_job
2013-10-25 14:24:59 +01:00
if destroy_from_resque_queue == 1
build_canceled
else
send_stop_signal
2012-12-28 14:00:37 +00:00
end
true
end
2012-12-28 14:00:37 +00:00
2013-10-25 14:24:59 +01:00
def destroy_from_resque_queue
result = Resque::Job.destroy(
2013-10-25 14:24:59 +01:00
worker_queue_with_priority,
worker_queue_class,
abf_worker_args
)
cleanup_build_sets
result
2013-10-25 14:24:59 +01:00
end
def worker_queue_with_priority(prefix = true)
queue = ''
if prefix && is_a?(BuildList)
if mass_build_id
queue << "mass_build_#{mass_build_id}_"
else
queue << "user_build_#{user_id}_"
end
end
queue << abf_worker_base_queue
queue << '_' << abf_worker_priority if abf_worker_priority.present?
queue
end
def worker_queue_class
"AbfWorker::#{abf_worker_base_queue.classify}#{abf_worker_priority.capitalize}"
end
private
def cleanup_build_sets
return unless is_a?(BuildList)
if Resque.redis.llen("queue:#{worker_queue_with_priority}") == 0
if mass_build_id
Resque.redis.srem MASS_BUILDS_SET, mass_build_id
else
Resque.redis.srem USER_BUILDS_SET, user_id
end
Resque.redis.del "queue:#{worker_queue_with_priority}"
end
end
def update_build_sets
return unless is_a?(BuildList)
if mass_build_id
Resque.redis.sadd MASS_BUILDS_SET, mass_build_id
else
Resque.redis.sadd USER_BUILDS_SET, user_id
end
end
def send_stop_signal
Resque.redis.setex(
"#{service_queue}::live-inspector",
240, # Data will be removed from Redis after 240 sec.
'USR1' # Immediately kill child but don't exit
)
end
def service_queue
"abfworker::#{abf_worker_base_queue.gsub(/\_/, '-')}-#{id}"
end
end