From e3c3b33ea7a5a223e22688c3f0eb2d3dab9f991c Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 15 Dec 2019 09:51:28 +0000 Subject: Graceful: Xorm, RepoIndexer, Cron and Others (#9282) * Change graceful to use a singleton obtained through GetManager instead of a global. * Graceful: Make TestPullRequests shutdownable * Graceful: Make the cron tasks graceful * Graceful: AddTestPullRequest run in graceful ctx * Graceful: SyncMirrors shutdown * Graceful: SetDefaultContext for Xorm to be HammerContext * Avoid starting graceful for migrate commands and checkout * Graceful: DeliverHooks now can be shutdown * Fix multiple syncing errors in modules/sync/UniqueQueue & Make UniqueQueue closable * Begin the process of making the repo indexer shutdown gracefully --- modules/cron/cron.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'modules/cron') diff --git a/modules/cron/cron.go b/modules/cron/cron.go index 795fafb51f..f4511a8e79 100644 --- a/modules/cron/cron.go +++ b/modules/cron/cron.go @@ -6,9 +6,11 @@ package cron import ( + "context" "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/setting" @@ -37,17 +39,19 @@ var taskStatusTable = sync.NewStatusTable() type Func func() // WithUnique wrap a cron func with an unique running check -func WithUnique(name string, body Func) Func { +func WithUnique(name string, body func(context.Context)) Func { return func() { if !taskStatusTable.StartIfNotRunning(name) { return } defer taskStatusTable.Stop(name) - body() + graceful.GetManager().RunWithShutdownContext(body) } } // NewContext begins cron tasks +// Each cron task is run within the shutdown context as a running server +// AtShutdown the cron server is stopped func NewContext() { var ( entry *cron.Entry @@ -129,6 +133,7 @@ func NewContext() { go WithUnique(updateMigrationPosterID, migrations.UpdateMigrationPosterID)() c.Start() + graceful.GetManager().RunAtShutdown(context.Background(), c.Stop) } // ListTasks returns all running cron tasks. -- cgit v1.2.3