Skip to content

Commit

Permalink
[api] Move ProjectLogRotate to 1 event per job
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana06 committed Dec 8, 2017
1 parent 4e6efd8 commit 886c5a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
40 changes: 4 additions & 36 deletions src/api/app/jobs/project_log_rotate_job.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
class ProjectLogRotateJob < ApplicationJob
queue_as :project_log_rotate

def perform
# Package and Project events
event_types = ["Event::BranchCommand",
"Event::Build",
"Event::CommentForPackage",
"Event::Commit",
"Event::CreatePackage",
"Event::DeletePackage",
"Event::ServiceFail",
"Event::ServiceSuccess",
"Event::UndeletePackage",
"Event::UpdatePackage",
"Event::Upload",
"Event::VersionChange",
"Event::CommentForProject",
"Event::CreateProject",
"Event::DeleteProject",
"Event::UndeleteProject",
"Event::UpdateProjectConfig",
"Event::UpdateProject"]
oldest_date = 10.days.ago

# First, skip old events and mark them all as "logged" (even those that
# don't belong to the event_classes)
Event::Base.where(project_logged: false).where(["created_at < ?", oldest_date]).update_all(project_logged: true)

# Create log entries based on the events (but this time, only those in event_classes)
Event::Base.where(project_logged: false, eventtype: event_types).find_in_batches(batch_size: 10000) do |events_batch|
events_batch.each do |event|
entry = ProjectLogEntry.create_from(event)
event.update_attributes(project_logged: true) if entry.persisted?
end
end

# Clean up old entries
ProjectLogEntry.clean_older_than oldest_date
def perform(event_id)
event = Event::Base.find(event_id)
entry = ProjectLogEntry.create_from(event)
event.update_attributes(project_logged: true) if entry.persisted?
end
end
18 changes: 18 additions & 0 deletions src/api/app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
module Event
PROJECT_CLASSES = ["Event::CommentForProject",
"Event::CreateProject",
"Event::DeleteProject",
"Event::UndeleteProject",
"Event::UpdateProjectConfig",
"Event::UpdateProject"].freeze
PACKAGE_CLASSES = ["Event::BranchCommand",
"Event::Build",
"Event::CommentForPackage",
"Event::Commit",
"Event::CreatePackage",
"Event::DeletePackage",
"Event::ServiceFail",
"Event::ServiceSuccess",
"Event::UndeletePackage",
"Event::UpdatePackage",
"Event::Upload",
"Event::VersionChange"].freeze
end
5 changes: 5 additions & 0 deletions src/api/app/models/event/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Base < ApplicationRecord
self.table_name = 'events'

before_save :shorten_payload_if_necessary
after_create :create_project_log_rotate_job, if: -> { (PROJECT_CLASSES | PACKAGE_CLASSES).include?(self.class.name) }

EXPLANATION_FOR_NOTIFICATIONS = {
'Event::BuildFail' => 'Receive notifications of build failures for packages for which you are...',
Expand Down Expand Up @@ -158,6 +159,10 @@ def payload
@payload ||= Yajl::Parser.parse(read_attribute(:payload))
end

def create_project_log_rotate_job
ProjectLogRotateJob.perform_later(id)
end

after_create :perform_create_jobs

def perform_create_jobs
Expand Down
4 changes: 0 additions & 4 deletions src/api/config/clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ module Clockwork
SendEventEmailsJob.perform_later
end

every(10.minutes, 'project log rotates') do
ProjectLogRotateJob.perform_later
end

every(49.minutes, 'rescale history') do
StatusHistoryRescalerJob.perform_later
end
Expand Down

0 comments on commit 886c5a4

Please sign in to comment.