2014-06-20 19:02:32 +01:00
|
|
|
module AbfWorkerMethods
|
|
|
|
extend ActiveSupport::Concern
|
2012-12-06 14:41:24 +00:00
|
|
|
|
2014-02-04 22:43:01 +00:00
|
|
|
MASS_BUILDS_SET = 'abf-worker::mass-builds'
|
|
|
|
USER_BUILDS_SET = 'abf-worker::user-builds'
|
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
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']
|
2012-12-06 14:41:24 +00:00
|
|
|
)
|
|
|
|
end
|
2014-02-04 22:43:01 +00:00
|
|
|
|
2014-06-20 19:02:32 +01:00
|
|
|
def next_build
|
2014-02-04 22:43:01 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2013-07-30 13:39:43 +01:00
|
|
|
end
|
2012-12-06 14:41:24 +00:00
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
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
|
2014-02-04 22:43:01 +00:00
|
|
|
update_build_sets
|
2013-07-30 13:39:43 +01:00
|
|
|
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
|
2014-02-04 22:43:01 +00:00
|
|
|
update_build_sets
|
2014-03-18 21:52:40 +00:00
|
|
|
Redis.current.lpush "resque:queue:#{worker_queue_with_priority}",
|
2013-11-29 12:58:37 +00:00
|
|
|
Resque.encode({'class' => worker_queue_class, 'args' => [abf_worker_args]})
|
|
|
|
end
|
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
def cancel_job
|
2013-10-25 14:24:59 +01:00
|
|
|
if destroy_from_resque_queue == 1
|
2013-07-30 13:39:43 +01:00
|
|
|
build_canceled
|
|
|
|
else
|
|
|
|
send_stop_signal
|
2012-12-28 14:00:37 +00:00
|
|
|
end
|
2013-07-30 13:39:43 +01:00
|
|
|
true
|
|
|
|
end
|
2012-12-28 14:00:37 +00:00
|
|
|
|
2013-10-25 14:24:59 +01:00
|
|
|
def destroy_from_resque_queue
|
2014-02-24 20:59:20 +00:00
|
|
|
Resque::Job.destroy(
|
2013-10-25 14:24:59 +01:00
|
|
|
worker_queue_with_priority,
|
|
|
|
worker_queue_class,
|
|
|
|
abf_worker_args
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-02-04 22:43:01 +00:00
|
|
|
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
|
2013-07-30 13:39:43 +01:00
|
|
|
queue << '_' << abf_worker_priority if abf_worker_priority.present?
|
|
|
|
queue
|
|
|
|
end
|
2012-12-06 14:41:24 +00:00
|
|
|
|
2014-02-04 22:43:01 +00:00
|
|
|
def worker_queue_class
|
|
|
|
"AbfWorker::#{abf_worker_base_queue.classify}#{abf_worker_priority.capitalize}"
|
2013-07-30 13:39:43 +01:00
|
|
|
end
|
2012-12-06 14:41:24 +00:00
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
private
|
|
|
|
|
2014-02-04 22:43:01 +00:00
|
|
|
|
|
|
|
def update_build_sets
|
|
|
|
return unless is_a?(BuildList)
|
|
|
|
|
2014-02-04 22:49:19 +00:00
|
|
|
key = mass_build_id ? MASS_BUILDS_SET : USER_BUILDS_SET
|
2014-03-18 21:52:40 +00:00
|
|
|
Redis.current.pipelined do
|
|
|
|
Redis.current.sadd key, mass_build_id || user_id
|
|
|
|
Redis.current.sadd 'resque:queues', worker_queue_with_priority
|
2014-02-24 20:59:20 +00:00
|
|
|
end
|
2014-02-04 22:43:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
def send_stop_signal
|
2014-03-18 21:52:40 +00:00
|
|
|
Redis.current.setex(
|
2013-07-30 13:39:43 +01:00
|
|
|
"#{service_queue}::live-inspector",
|
|
|
|
240, # Data will be removed from Redis after 240 sec.
|
|
|
|
'USR1' # Immediately kill child but don't exit
|
|
|
|
)
|
|
|
|
end
|
2012-12-06 14:41:24 +00:00
|
|
|
|
2013-07-30 13:39:43 +01:00
|
|
|
def service_queue
|
|
|
|
"abfworker::#{abf_worker_base_queue.gsub(/\_/, '-')}-#{id}"
|
2012-12-06 14:41:24 +00:00
|
|
|
end
|
2013-07-30 13:39:43 +01:00
|
|
|
|
2014-06-20 19:02:32 +01:00
|
|
|
|
|
|
|
end
|