Everytime git-mirror get new_refs or just git fetch, these actions and
the time will be recorded in hash fork_stat{}. But when container git-mirror restart,
fork_stat{} will be initialized. So historical data can't be kept.
Now, git-mirror will get historical data from es when initialize hash
fork_stat{}. So historical data will be kept.
Signed-off-by: Li Yuanchao <lyc163mail(a)163.com>
---
lib/git_mirror.rb | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/lib/git_mirror.rb b/lib/git_mirror.rb
index 86f02e5..6bb474a 100644
--- a/lib/git_mirror.rb
+++ b/lib/git_mirror.rb
@@ -109,15 +109,8 @@ class MirrorMain
@webhook_queue = channel.queue('web_hook')
end
- def fork_stat_init(stat_key)
- @fork_stat[stat_key] = {
- queued: false,
- priority: 0,
- fetch_time: [],
- offset_fetch: 0,
- new_refs_time: [],
- offset_new_refs: 0
- }
+ def fork_stat_init(git_repo)
+ @fork_stat[git_repo] = get_fork_stat(git_repo)
end
def load_defaults(repodir)
@@ -372,4 +365,28 @@ class MirrorMain
handle_submodule(submodule)
end
+
+ def copy_from_es(es_data)
+ fork_stat = {}
+ es_data.each do |key, value|
+ fork_stat[key.to_sym] = value
+ end
+ return fork_stat
+ end
+
+ def get_fork_stat(git_repo)
+ query = { query: { match: { git_repo: git_repo } } }
+ result = @es_client.search(index: 'repo', body: query)['hits']
+ return copy_from_es(result['hits'][0]['_source']) if result['total'].positive?
+
+ fork_stat = {
+ queued: false,
+ priority: 0,
+ fetch_time: [],
+ offset_fetch: 0,
+ new_refs_time: [],
+ offset_new_refs: 0
+ }
+ return fork_stat
+ end
end
--
2.23.0