Skip to content

Commit 33aafdd

Browse files
committed
Performance fix for redmine_git_hosting.
This prevents a rediculous number of calls to update_repositories caused by the fact that redmine saves to the repository model for every commit that it examines. This patch also prevents multiple calls to update_repositories when adding groups to the membership of a project. You need to migrate plugins to take advantage of new indices: rake db:migrate_plugins
1 parent 98cbb85 commit 33aafdd

7 files changed

Lines changed: 130 additions & 5 deletions

File tree

app/models/git_hosting_observer.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class GitHostingObserver < ActiveRecord::Observer
22
observe :project, :user, :gitolite_public_key, :member, :role, :repository
33

44
@@updating_active = true
5+
@@updating_active_stack = 0
56
@@cached_project_updates = []
67

78
def reload_this_observer
@@ -12,14 +13,25 @@ def reload_this_observer
1213

1314

1415
def self.set_update_active(is_active)
15-
@@updating_active = is_active
16-
if is_active
16+
if !is_active
17+
@@updating_active_stack += 1
18+
else
19+
@@updating_active_stack -= 1
20+
if @@updating_active_stack < 0
21+
@@updating_active_stack = 0
22+
end
23+
end
24+
25+
if is_active && @@updating_active_stack == 0
1726
if @@cached_project_updates.length > 0
1827
@@cached_project_updates = @@cached_project_updates.flatten.uniq.compact
1928
GitHosting::update_repositories(@@cached_project_updates, false)
29+
@@cached_project_updates = []
2030
end
31+
@@updating_active = true
32+
else
33+
@@updating_active = false
2134
end
22-
@@cached_project_updates = []
2335
end
2436

2537

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class AddIndexesToGitolitePublicKey < ActiveRecord::Migration
2+
def self.up
3+
add_index :gitolite_public_keys, :user_id
4+
add_index :gitolite_public_keys, :identifier
5+
end
6+
7+
def self.down
8+
remove_index :gitolite_public_keys, :user_id
9+
remove_index :gitolite_public_keys, :identifier
10+
end
11+
end

init.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666
require 'git_hosting/patches/git_repository_patch'
6767
Repository::Git.send(:include, GitHosting::Patches::GitRepositoryPatch)
6868

69+
require_dependency 'sys_controller'
70+
require 'git_hosting/patches/sys_controller_patch'
71+
SysController.send(:include, GitHosting::Patches::SysControllerPatch)
72+
73+
require_dependency 'members_controller'
74+
require 'git_hosting/patches/members_controller_patch'
75+
MembersController.send(:include, GitHosting::Patches::MembersControllerPatch)
76+
6977
require_dependency 'git_hosting/patches/repository_cia_filters'
7078
end
7179

lib/git_hosting/patches/git_repository_patch.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ def extra_report_last_commit_with_always_true
99
true
1010
end
1111

12+
def fetch_changesets_with_disable_update
13+
# Turn of updates during repository update
14+
GitHostingObserver.set_update_active(false);
15+
16+
# Do actual update
17+
fetch_changesets_without_disable_update
18+
19+
# Reenable updates to perform a single update
20+
GitHostingObserver.set_update_active(true);
21+
end
1222

1323
def self.included(base)
1424
base.class_eval do
@@ -17,6 +27,7 @@ def self.included(base)
1727
begin
1828
base.send(:alias_method_chain, :report_last_commit, :always_true)
1929
base.send(:alias_method_chain, :extra_report_last_commit, :always_true)
30+
base.send(:alias_method_chain, :fetch_changesets, :disable_update)
2031
rescue
2132
end
2233

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module GitHosting
2+
module Patches
3+
module MembersControllerPatch
4+
def new_with_disable_update
5+
# Turn of updates during repository update
6+
GitHostingObserver.set_update_active(false);
7+
8+
# Do actual update
9+
new_without_disable_update
10+
11+
# Reenable updates to perform a single update
12+
GitHostingObserver.set_update_active(true);
13+
end
14+
def edit_with_disable_update
15+
# Turn of updates during repository update
16+
GitHostingObserver.set_update_active(false);
17+
18+
# Do actual update
19+
edit_without_disable_update
20+
21+
# Reenable updates to perform a single update
22+
GitHostingObserver.set_update_active(true);
23+
end
24+
def destroy_with_disable_update
25+
# Turn of updates during repository update
26+
GitHostingObserver.set_update_active(false);
27+
28+
# Do actual update
29+
destroy_without_disable_update
30+
31+
# Reenable updates to perform a single update
32+
GitHostingObserver.set_update_active(true);
33+
end
34+
35+
def self.included(base)
36+
base.class_eval do
37+
unloadable
38+
end
39+
begin
40+
base.send(:alias_method_chain, :new, :disable_update)
41+
base.send(:alias_method_chain, :edit, :disable_update)
42+
base.send(:alias_method_chain, :destroy, :disable_update)
43+
rescue
44+
end
45+
end
46+
end
47+
end
48+
end

lib/git_hosting/patches/repository_patch.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,27 @@ def factory_with_git_extra_init(klass_name, *args)
2525
end
2626
return new_repo
2727
end
28-
end
28+
def fetch_changesets_with_disable_update
29+
# Turn of updates during repository update
30+
GitHostingObserver.set_update_active(false);
31+
32+
# Do actual update
33+
fetch_changesets_without_disable_update
2934

35+
# Reenable updates to perform a single update
36+
GitHostingObserver.set_update_active(true);
37+
end
38+
end
3039

3140
def self.included(base)
3241
base.extend(ClassMethods)
3342
base.class_eval do
3443
unloadable
3544
class << self
3645
alias_method_chain :factory, :git_extra_init
46+
alias_method_chain :fetch_changesets, :disable_update
3747
end
3848
end
39-
4049
end
4150
end
4251
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module GitHosting
2+
module Patches
3+
module SysControllerPatch
4+
def fetch_changesets_with_disable_update
5+
# Turn of updates during repository update
6+
GitHostingObserver.set_update_active(false);
7+
8+
# Do actual update
9+
fetch_changesets_without_disable_update
10+
11+
# Reenable updates to perform a single update
12+
GitHostingObserver.set_update_active(true);
13+
end
14+
15+
def self.included(base)
16+
base.class_eval do
17+
unloadable
18+
end
19+
begin
20+
base.send(:alias_method_chain, :fetch_changesets, :disable_update)
21+
rescue
22+
end
23+
end
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)