rosa-build/app/controllers/api/v1/jobs_controller.rb

141 lines
4.7 KiB
Ruby
Raw Normal View History

2013-09-30 17:35:56 +01:00
class Api::V1::JobsController < Api::V1::BaseController
2013-10-17 19:22:40 +01:00
# QUEUES = %w(iso_worker_observer publish_observer rpm_worker_observer)
# QUEUE_CLASSES = %w(AbfWorker::IsoWorkerObserver AbfWorker::PublishObserver AbfWorker::RpmWorkerObserver)
QUEUES = %w(rpm_worker_observer)
QUEUE_CLASSES = %w(AbfWorker::RpmWorkerObserver)
2013-09-30 17:35:56 +01:00
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
skip_after_action :verify_authorized
2013-09-30 17:35:56 +01:00
def shift
2016-06-10 14:21:42 +01:00
clear_stale_builders
2016-03-22 13:12:27 +00:00
job_shift_sem = Redis::Semaphore.new(:job_shift_lock)
job_shift_sem.lock
uid = BuildList.scoped_to_arch(arch_ids).
for_status([BuildList::BUILD_PENDING, BuildList::RERUN_TESTS]).
2016-03-22 13:12:27 +00:00
for_platform(platform_ids).where(builder: nil).pluck('DISTINCT user_id').sample
if uid
if native_arch_ids.empty?
build_lists = BuildList.scoped_to_arch(arch_ids).for_platform(platform_ids).
where(native_build: false)
else
sql_normal = []
params = []
sql_normal << 'arch_id IN (?)' if !arch_ids.empty?
params << arch_ids if !arch_ids.empty?
sql_normal << 'native_build=false'
sql_normal *= ' AND '
sql_native_arch = 'arch_id IN (?)'
params << native_arch_ids
sql_native_arch << ' AND native_build=true'
2016-03-29 13:46:08 +01:00
build_lists = BuildList.where(sql_normal + ' OR ' + sql_native_arch, *params).for_platform(platform_ids)
end
build_lists = build_lists.for_status([BuildList::BUILD_PENDING, BuildList::RERUN_TESTS]).
where(user_id: uid).where(builder: nil).oldest.order(:created_at)
2016-03-22 13:12:27 +00:00
if current_user.system?
@build_list = build_lists.where(external_nodes: ["", nil]).first
@build_list ||= build_lists.external_nodes(:everything).first
else
@build_list = build_lists.external_nodes(:owned).for_user(current_user).first
@build_list ||= BuildListPolicy::Scope.new(current_user, build_lists).owned.
external_nodes(:everything).readonly(false).first
end
2016-03-22 13:12:27 +00:00
set_builder
2013-09-30 17:35:56 +01:00
end
2016-03-22 13:12:27 +00:00
job_shift_sem.unlock
job = {
worker_queue: @build_list.worker_queue_with_priority(false),
worker_class: @build_list.worker_queue_class,
:worker_args => [@build_list.abf_worker_args]
} if @build_list
2014-01-21 04:51:49 +00:00
render json: { job: job }.to_json
2013-09-30 17:35:56 +01:00
end
def statistics
if params[:uid].present?
RpmBuildNode.create(
id: params[:uid],
user_id: current_user.id,
system: current_user.system?,
worker_count: params[:worker_count],
busy_workers: params[:busy_workers],
host: params[:host],
2016-07-11 17:28:38 +01:00
query_string: params[:query_string].to_s
) rescue nil
end
2014-01-21 04:51:49 +00:00
render nothing: true
end
2013-09-30 17:35:56 +01:00
def status
2014-03-05 22:18:19 +00:00
if params[:key] =~ /\Aabfworker::(rpm|iso)-worker-[\d]+::live-inspector\z/
status = Redis.current.get(params[:key])
2014-03-05 22:18:19 +00:00
end
render json: { status: status }.to_json
2013-09-30 17:35:56 +01:00
end
def logs
name = params[:name]
if name =~ /abfworker::rpm-worker/
2014-01-21 04:51:49 +00:00
if current_user.system? || current_user.id == BuildList.where(id: name.gsub(/[^\d]/, '')).first.try(:builder_id)
2016-03-08 02:40:23 +00:00
BuildList.log_server.setex name, 15, params[:logs]
end
end
2014-01-21 04:51:49 +00:00
render nothing: true
end
2013-09-30 17:35:56 +01:00
def feedback
2013-10-02 16:46:40 +01:00
worker_queue = params[:worker_queue]
worker_class = params[:worker_class]
2013-10-18 14:17:29 +01:00
if QUEUES.include?(worker_queue) && QUEUE_CLASSES.include?(worker_class)
2013-10-17 19:22:40 +01:00
worker_args = (params[:worker_args] || []).first || {}
2014-01-21 04:51:49 +00:00
worker_args = worker_args.merge(feedback_from_user: current_user.id)
2016-05-28 19:21:02 +01:00
Sidekiq::Client.push 'queue' => worker_queue, 'class' => worker_class, 'args' => [worker_args]
2014-01-21 04:51:49 +00:00
render nothing: true
2013-09-30 17:35:56 +01:00
else
2014-01-21 04:51:49 +00:00
render nothing: true, status: 403
2013-09-30 17:35:56 +01:00
end
end
2014-04-15 19:08:43 +01:00
protected
2016-06-10 14:21:42 +01:00
def clear_stale_builders
BuildList.transaction do
BuildList.where(["updated_at < ?", 120.seconds.ago]).where(status: BuildList::BUILD_PENDING).where.not(builder: nil).find_each(batch_size: 50) do |bl|
2016-06-10 19:56:03 +01:00
bl.update_column(:builder_id, nil)
2016-06-10 14:21:42 +01:00
end
end
end
def platform_ids
@platform_ids ||= begin
platforms = params[:platforms].to_s.split(',')
platforms.present? ? Platform.where(name: platforms).pluck(:id) : []
end
end
def arch_ids
@arch_ids ||= begin
arches = params[:arches].to_s.split(',')
arches.present? ? Arch.where(name: arches).pluck(:id) : []
end
end
def native_arch_ids
@native_arch_ids ||= begin
arches = params[:arches].to_s.split(',')
native_arches = params[:native_arches].to_s.split(',')
native_arches &= arches if !arches.empty?
native_arches.present? ? Arch.where(name: native_arches).pluck(:id) : []
end
end
2014-04-15 19:08:43 +01:00
def set_builder
return unless @build_list
@build_list.builder = current_user
@build_list.save
end
2013-09-30 17:35:56 +01:00
end