cron.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cron
  5. import (
  6. "time"
  7. log "unknwon.dev/clog/v2"
  8. "github.com/gogs/cron"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/conf"
  11. )
  12. var c = cron.New()
  13. func NewContext() {
  14. var (
  15. entry *cron.Entry
  16. err error
  17. )
  18. if conf.Cron.UpdateMirror.Enabled {
  19. entry, err = c.AddFunc("Update mirrors", conf.Cron.UpdateMirror.Schedule, db.MirrorUpdate)
  20. if err != nil {
  21. log.Fatal("Cron.(update mirrors): %v", err)
  22. }
  23. if conf.Cron.UpdateMirror.RunAtStart {
  24. entry.Prev = time.Now()
  25. entry.ExecTimes++
  26. go db.MirrorUpdate()
  27. }
  28. }
  29. if conf.Cron.RepoHealthCheck.Enabled {
  30. entry, err = c.AddFunc("Repository health check", conf.Cron.RepoHealthCheck.Schedule, db.GitFsck)
  31. if err != nil {
  32. log.Fatal("Cron.(repository health check): %v", err)
  33. }
  34. if conf.Cron.RepoHealthCheck.RunAtStart {
  35. entry.Prev = time.Now()
  36. entry.ExecTimes++
  37. go db.GitFsck()
  38. }
  39. }
  40. if conf.Cron.CheckRepoStats.Enabled {
  41. entry, err = c.AddFunc("Check repository statistics", conf.Cron.CheckRepoStats.Schedule, db.CheckRepoStats)
  42. if err != nil {
  43. log.Fatal("Cron.(check repository statistics): %v", err)
  44. }
  45. if conf.Cron.CheckRepoStats.RunAtStart {
  46. entry.Prev = time.Now()
  47. entry.ExecTimes++
  48. go db.CheckRepoStats()
  49. }
  50. }
  51. if conf.Cron.RepoArchiveCleanup.Enabled {
  52. entry, err = c.AddFunc("Repository archive cleanup", conf.Cron.RepoArchiveCleanup.Schedule, db.DeleteOldRepositoryArchives)
  53. if err != nil {
  54. log.Fatal("Cron.(repository archive cleanup): %v", err)
  55. }
  56. if conf.Cron.RepoArchiveCleanup.RunAtStart {
  57. entry.Prev = time.Now()
  58. entry.ExecTimes++
  59. go db.DeleteOldRepositoryArchives()
  60. }
  61. }
  62. c.Start()
  63. }
  64. // ListTasks returns all running cron tasks.
  65. func ListTasks() []*cron.Entry {
  66. return c.Entries()
  67. }