Commit 021d53c9 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Run 'git gc' every 10 pushes

parent a02fe251
......@@ -49,6 +49,8 @@ class GitPushService < BaseService
# Update merge requests that may be affected by this push. A new branch
# could cause the last commit of a merge request to change.
update_merge_requests
perform_housekeeping
end
def update_main_language
......@@ -73,6 +75,12 @@ class GitPushService < BaseService
ProjectCacheWorker.perform_async(@project.id)
end
def perform_housekeeping
housekeeping = Projects::HousekeepingService.new(@project)
housekeeping.increment!
housekeeping.execute if housekeeping.needed?
end
def process_default_branch
@push_commits = project.repository.commits(params[:newrev])
......
......@@ -19,9 +19,21 @@ module Projects
if !try_obtain_lease
return "Housekeeping was already triggered in the past #{LEASE_TIMEOUT / 60} minutes"
end
GitlabShellWorker.perform_async(:gc, @project.path_with_namespace)
return "Housekeeping successfully started"
@project.pushes_since_gc = 0
@project.save!
"Housekeeping successfully started"
end
def needed?
@project.pushes_since_gc >= 10
end
def increment!
@project.pushes_since_gc += 1
@project.save!
end
private
......
class ProjectsAddPushesSinceGc < ActiveRecord::Migration
def change
add_column :projects, :pushes_since_gc, :integer, default: 0
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160309140734) do
ActiveRecord::Schema.define(version: 20160314143402) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -711,6 +711,7 @@ ActiveRecord::Schema.define(version: 20160309140734) do
t.boolean "pending_delete", default: false
t.boolean "public_builds", default: true, null: false
t.string "main_language"
t.integer "pushes_since_gc", default: 0
end
add_index "projects", ["builds_enabled", "shared_runners_enabled"], name: "index_projects_on_builds_enabled_and_shared_runners_enabled", using: :btree
......
......@@ -401,6 +401,33 @@ describe GitPushService, services: true do
end
end
describe "housekeeping" do
let(:housekeeping) { instance_double('Projects::HousekeepingService', increment!: nil, needed?: false) }
before do
allow(Projects::HousekeepingService).to receive(:new).and_return(housekeeping)
end
it 'does not perform housekeeping when not needed' do
expect(housekeeping).not_to receive(:execute)
execute_service(project, user, @oldrev, @newrev, @ref)
end
it 'performs housekeeping when needed' do
expect(housekeeping).to receive(:needed?).and_return(true)
expect(housekeeping).to receive(:execute)
execute_service(project, user, @oldrev, @newrev, @ref)
end
it 'increments the push counter' do
expect(housekeeping).to receive(:increment!)
execute_service(project, user, @oldrev, @newrev, @ref)
end
end
def execute_service(project, user, oldrev, newrev, ref)
service = described_class.new(project, user, oldrev: oldrev, newrev: newrev, ref: ref )
service.execute
......
......@@ -37,4 +37,12 @@ describe Projects::HousekeepingService do
expect(subject.needed?).to eq(true)
end
end
end
\ No newline at end of file
describe :increment! do
it 'increments the pushes_since_gc counter' do
expect(project.pushes_since_gc).to eq(0)
subject.increment!
expect(project.pushes_since_gc).to eq(1)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment