From c263df2fecd6ec70521209a99b77a3fead1b9a62 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 10 Jul 2014 00:24:41 +0400 Subject: [PATCH] #405: Run mass builds with relations --- app/jobs/run_extra_mass_builds_job.rb | 31 ++++++++++++++++++ app/models/build_list.rb | 4 +-- app/models/mass_build.rb | 32 ++++++++++++++++++- config/resque_schedule.yml | 7 ++++ ...0140709194335_add_status_to_mass_builds.rb | 13 ++++++++ db/schema.rb | 3 +- 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 app/jobs/run_extra_mass_builds_job.rb create mode 100644 db/migrate/20140709194335_add_status_to_mass_builds.rb diff --git a/app/jobs/run_extra_mass_builds_job.rb b/app/jobs/run_extra_mass_builds_job.rb new file mode 100644 index 000000000..9ce0c933c --- /dev/null +++ b/app/jobs/run_extra_mass_builds_job.rb @@ -0,0 +1,31 @@ +class RunExtraMassBuildsJob + @queue = :low + + def self.perform + RunExtraMassBuildsJob.new.perform + end + + def perform + MassBuild.where(status: BUILD_PENDING).find_each do |mb| + next if mb.extra_mass_builds.blank? + next if mb.extra_mass_builds.exclude?(mass_build_id) + emb = MassBuild.where(status: SUCCESS, id: mb.extra_mass_builds).to_a + next if emb.size != mb.extra_mass_builds.size + + next if emb.find{ |mb| not_ready?(mb) } + mb.build_all + end + end + + private + + # Returns true if mass build has not published packages or packages without container + def not_ready?(mb) + mb.build_lists.count != mb.build_lists.where( + 'status = ? OR container_status = ?', + BuildList::BUILD_PUBLISHED, + BuildList::BUILD_PUBLISHED + ).count + end + +end \ No newline at end of file diff --git a/app/models/build_list.rb b/app/models/build_list.rb index b149e55ab..ecc36ff0b 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -15,7 +15,7 @@ class BuildList < ActiveRecord::Base belongs_to :builder, class_name: 'User' belongs_to :publisher, class_name: 'User' belongs_to :advisory - belongs_to :mass_build, counter_cache: true, touch: true + belongs_to :mass_build, counter_cache: true has_many :items, class_name: '::BuildList::Item', dependent: :destroy has_many :packages, class_name: '::BuildList::Package', dependent: :destroy has_many :source_packages, -> { where(package_type: 'source') }, class_name: '::BuildList::Package' @@ -172,7 +172,7 @@ class BuildList < ActiveRecord::Base end after_transition on: :published, - do: [:set_version_and_tag, :actualize_packages] + do: %i(set_version_and_tag actualize_packages) after_transition on: :publish, do: :set_publisher after_transition(on: :publish) do |build_list, transition| if transition.from == BUILD_PUBLISHED_INTO_TESTING diff --git a/app/models/mass_build.rb b/app/models/mass_build.rb index 8b563fc52..1bf99f8a9 100644 --- a/app/models/mass_build.rb +++ b/app/models/mass_build.rb @@ -2,6 +2,34 @@ class MassBuild < ActiveRecord::Base AUTO_PUBLISH_STATUSES = %w(none default testing) + STATUSES, HUMAN_STATUSES = [], {} + [ + %w(SUCCESS 0), + %w(BUILD_STARTED 3000), + %w(BUILD_PENDING 2000), + ].each do |kind, value| + value = value.to_i + const_set kind, value + STATUSES << value + HUMAN_STATUSES[value] = kind.downcase.to_sym + end + STATUSES.freeze + HUMAN_STATUSES.freeze + + state_machine :status, initial: :build_pending do + event :start do + transition build_pending: :build_started + end + + event :done do + transition build_started: :success + end + + HUMAN_STATUSES.each do |code,name| + state name, value: code + end + end + belongs_to :build_for_platform, -> { where(platform_type: 'main') }, class_name: 'Platform' belongs_to :save_to_platform, class_name: 'Platform' belongs_to :user @@ -43,7 +71,7 @@ class MassBuild < ActiveRecord::Base :use_extra_tests, inclusion: { in: [true, false] } - after_commit :build_all, on: :create + after_commit :build_all, on: :create, if: Proc.new { |mb| mb.extra_mass_builds.blank? } before_validation :set_data, on: :create COUNT_STATUSES = %i( @@ -58,6 +86,7 @@ class MassBuild < ActiveRecord::Base ) def build_all + start # later with resque arches_list = arch_names ? Arch.where(name: arch_names.split(', ')) : Arch.all @@ -82,6 +111,7 @@ class MassBuild < ActiveRecord::Base update_column :missed_projects_list, list end end + done end later :build_all, queue: :low diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index 600d5a7f0..dc911c9a1 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -26,6 +26,13 @@ clean_api_defender_statistics: queue: low description: 'Cleans ApiDefender statistics' +run_extra_mass_builds: + every: + - '5m' + class: 'RunExtraMassBuildsJob' + queue: low + description: 'Run mass builds with relations' + restart_nodes: every: - '5m' diff --git a/db/migrate/20140709194335_add_status_to_mass_builds.rb b/db/migrate/20140709194335_add_status_to_mass_builds.rb new file mode 100644 index 000000000..89f7b89b2 --- /dev/null +++ b/db/migrate/20140709194335_add_status_to_mass_builds.rb @@ -0,0 +1,13 @@ +class AddStatusToMassBuilds < ActiveRecord::Migration + + def up + # Sets SUCCESS for all current builds + add_column :mass_builds, :status, :integer, null: false, default: 0 # SUCCESS + # Sets BUILD_PENDING for all new builds + change_column :mass_builds, :status, :integer, null: false, default: 2000 # BUILD_PENDING + end + + def down + remove_column :mass_builds, :status + end +end diff --git a/db/schema.rb b/db/schema.rb index 6e02c3918..418f449e5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140701172739) do +ActiveRecord::Schema.define(version: 20140709194335) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -331,6 +331,7 @@ ActiveRecord::Schema.define(version: 20140701172739) do t.text "extra_mass_builds" t.boolean "include_testing_subrepository", default: false, null: false t.boolean "auto_create_container", default: false, null: false + t.integer "status", default: 2000, null: false end create_table "users", force: true do |t|