Skip to content

Commit

Permalink
Merge pull request #4197 from Ana06/project_log_rotate
Browse files Browse the repository at this point in the history
 [api] Move ProjectLogRotate to 1 event per job
  • Loading branch information
Ana06 committed Dec 11, 2017
2 parents 124683e + 676f495 commit 11c3444
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 88 deletions.
8 changes: 0 additions & 8 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,3 @@ Rails/Output:
- 'src/api/config/*'
- 'src/api/db/**/*'
- 'src/api/lib/**/*'

# Checks for the use of methods which skip validations.
Rails/SkipsModelValidations:
Exclude:
- 'src/api/db/migrate/*'
- 'src/api/app/jobs/*'
- 'src/api/spec/models/bs_request_action_spec.rb'
- 'src/api/spec/models/review_spec.rb'
34 changes: 23 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-12-04 09:56:31 +0000 using RuboCop version 0.51.0.
# on 2017-12-08 08:39:43 +0000 using RuboCop version 0.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -185,7 +185,7 @@ Layout/MultilineHashBraceLayout:
Layout/MultilineMethodCallBraceLayout:
Enabled: false

# Offense count: 29
# Offense count: 28
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Expand All @@ -198,7 +198,6 @@ Layout/MultilineMethodCallIndentation:
- 'src/api/app/jobs/project_create_auto_cleanup_requests.rb'
- 'src/api/app/mixins/has_relationships.rb'
- 'src/api/app/models/owner.rb'
- 'src/api/spec/controllers/webui/request_controller_spec.rb'
- 'src/api/spec/features/webui/projects_spec.rb'
- 'src/api/spec/models/repository_spec.rb'
- 'src/api/test/unit/issue_test.rb'
Expand All @@ -211,7 +210,7 @@ Layout/MultilineMethodCallIndentation:
Layout/MultilineOperationIndentation:
Enabled: false

# Offense count: 75
# Offense count: 76
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, SupportedStylesForEmptyBraces.
# SupportedStyles: space, no_space
Expand All @@ -228,7 +227,7 @@ Layout/SpaceInLambdaLiteral:
- 'src/api/app/models/package_issue.rb'
- 'src/api/config/routes.rb'

# Offense count: 125
# Offense count: 140
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SupportedStylesForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
Expand Down Expand Up @@ -385,7 +384,7 @@ Lint/UriEscapeUnescape:
Metrics/AbcSize:
Max: 248

# Offense count: 411
# Offense count: 413
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 988
Expand All @@ -400,11 +399,11 @@ Metrics/BlockNesting:
Metrics/ClassLength:
Max: 1157

# Offense count: 244
# Offense count: 243
Metrics/CyclomaticComplexity:
Max: 55

# Offense count: 958
# Offense count: 957
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 264
Expand Down Expand Up @@ -660,6 +659,19 @@ Rails/ReversibleMigration:
- 'src/api/db/migrate/20160824132643_fix_bs_request_counter.rb'
- 'src/api/db/migrate/20170103132257_change_project_package_name_to_string.rb'

# Offense count: 7
# Configuration parameters: Blacklist.
# Blacklist: decrement!, decrement_counter, increment!, increment_counter, toggle!, touch, update_all, update_attribute, update_column, update_columns, update_counters
Rails/SkipsModelValidations:
Exclude:
- 'src/api/db/migrate/20151030130011_mark_events.rb'
- 'src/api/db/migrate/20161128115942_add_when_attribute_to_bs_request.rb'
- 'src/api/db/migrate/20170621100321_add_channel_to_event_subscriptions.rb'
- 'src/api/db/migrate/20170630144825_convert_tokens_to_service_tokens.rb'
- 'src/api/lib/tasks/project_log_rotate_manually.rake'
- 'src/api/spec/models/bs_request_action_spec.rb'
- 'src/api/spec/models/review_spec.rb'

# Offense count: 98
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: strict, flexible
Expand Down Expand Up @@ -691,7 +703,7 @@ Style/BlockDelimiters:
Style/BracesAroundHashParameters:
Enabled: false

# Offense count: 83
# Offense count: 67
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Expand Down Expand Up @@ -767,7 +779,7 @@ Style/Dir:
Exclude:
- 'src/api/script/start_test_backend'

# Offense count: 634
# Offense count: 631
Style/Documentation:
Enabled: false

Expand Down Expand Up @@ -1054,7 +1066,7 @@ Style/StderrPuts:
Exclude:
- 'src/api/test/functional/webui/spider_test.rb'

# Offense count: 9303
# Offense count: 9358
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Expand Down
9 changes: 9 additions & 0 deletions src/api/app/jobs/create_project_log_entry_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateProjectLogEntryJob < ApplicationJob
queue_as :project_log_rotate

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
41 changes: 0 additions & 41 deletions src/api/app/jobs/project_log_rotate_job.rb

This file was deleted.

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_entry_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_entry_job
CreateProjectLogEntryJob.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
20 changes: 20 additions & 0 deletions src/api/lib/tasks/project_log_rotate_manually.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
desc 'Run project log rotate job manually'
task :project_log_rotate_manually do
event_types = Event::PROJECT_CLASSES | Event::PACKAGE_CLASSES
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 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
end
20 changes: 1 addition & 19 deletions src/api/script/delayed_job_stats.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
puts "-----------EVENT-STATS------------"
# 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"]
Event::PROJECT_CLASSES | Event::PACKAGE_CLASSES
oldest_date = 10.days.ago
project_logged = Event::Base.where(project_logged: false, eventtype: event_types).where(["created_at > ?", oldest_date]).count
mails_sent = Event::Base.where(mails_sent: false).count
Expand Down
6 changes: 1 addition & 5 deletions src/api/spec/jobs/project_log_rotate_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'rails_helper'

RSpec.describe ProjectLogRotateJob, type: :job do
include ActiveJob::TestHelper

RSpec.describe CreateProjectLogEntryJob, type: :job do
describe '#perform' do
let!(:user1) { create(:confirmed_user) }
let!(:user2) { create(:confirmed_user) }
Expand All @@ -19,8 +17,6 @@
end
end

subject! { ProjectLogRotateJob.new.perform }

it 'creates a ProjectLogEntry' do
expect(ProjectLogEntry.count).to eq(10)
end
Expand Down

0 comments on commit 11c3444

Please sign in to comment.