aboutsummaryrefslogtreecommitdiffstats
path: root/services/cron/tasks.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-03-29 02:31:07 +0100
committerGitHub <noreply@github.com>2022-03-29 02:31:07 +0100
commit90e0a402c1185827fd3d5352ca915c531ce1e7b1 (patch)
treeaa27fa1774c1c3af797d86a08e829d30bdb8010a /services/cron/tasks.go
parente69b7a92ed91a4b078d216581a24497a72363494 (diff)
downloadgitea-90e0a402c1185827fd3d5352ca915c531ce1e7b1.tar.gz
gitea-90e0a402c1185827fd3d5352ca915c531ce1e7b1.zip
Show last cron messages on monitor page (#19223)
As discussed on #19221 we should store the results of the last task message on the crontask and show them on the monitor page. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'services/cron/tasks.go')
-rw-r--r--services/cron/tasks.go50
1 files changed, 37 insertions, 13 deletions
diff --git a/services/cron/tasks.go b/services/cron/tasks.go
index 070fb6e9e1..2252ad21e2 100644
--- a/services/cron/tasks.go
+++ b/services/cron/tasks.go
@@ -29,11 +29,14 @@ var (
// Task represents a Cron task
type Task struct {
- lock sync.Mutex
- Name string
- config Config
- fun func(context.Context, *user_model.User, Config) error
- ExecTimes int64
+ lock sync.Mutex
+ Name string
+ config Config
+ fun func(context.Context, *user_model.User, Config) error
+ Status string
+ LastMessage string
+ LastDoer string
+ ExecTimes int64
}
// DoRunAtStart returns if this task should run at the start
@@ -86,24 +89,45 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) {
}()
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
pm := process.GetManager()
- ctx, _, finished := pm.AddContext(baseCtx, config.FormatMessage(t.Name, "process", doer))
+ doerName := ""
+ if doer != nil && doer.ID != -1 {
+ doerName = doer.Name
+ }
+
+ ctx, _, finished := pm.AddContext(baseCtx, config.FormatMessage("en-US", t.Name, "process", doerName))
defer finished()
if err := t.fun(ctx, doer, config); err != nil {
+ var message string
+ var status string
if db.IsErrCancelled(err) {
- message := err.(db.ErrCancelled).Message
- if err := admin_model.CreateNotice(ctx, admin_model.NoticeTask, config.FormatMessage(t.Name, "aborted", doer, message)); err != nil {
- log.Error("CreateNotice: %v", err)
- }
- return
+ status = "cancelled"
+ message = err.(db.ErrCancelled).Message
+ } else {
+ status = "error"
+ message = err.Error()
}
- if err := admin_model.CreateNotice(ctx, admin_model.NoticeTask, config.FormatMessage(t.Name, "error", doer, err)); err != nil {
+
+ t.lock.Lock()
+ t.LastMessage = message
+ t.Status = status
+ t.LastDoer = doerName
+ t.lock.Unlock()
+
+ if err := admin_model.CreateNotice(ctx, admin_model.NoticeTask, config.FormatMessage("en-US", t.Name, "cancelled", doerName, message)); err != nil {
log.Error("CreateNotice: %v", err)
}
return
}
+
+ t.lock.Lock()
+ t.Status = "finished"
+ t.LastMessage = ""
+ t.LastDoer = doerName
+ t.lock.Unlock()
+
if config.DoNoticeOnSuccess() {
- if err := admin_model.CreateNotice(ctx, admin_model.NoticeTask, config.FormatMessage(t.Name, "finished", doer)); err != nil {
+ if err := admin_model.CreateNotice(ctx, admin_model.NoticeTask, config.FormatMessage("en-US", t.Name, "finished", doerName)); err != nil {
log.Error("CreateNotice: %v", err)
}
}