You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cron.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 The Gitea 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 admin
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/cron"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. // ListCronTasks api for getting cron tasks
  15. func ListCronTasks(ctx *context.APIContext) {
  16. // swagger:operation GET /admin/cron admin adminCronList
  17. // ---
  18. // summary: List cron tasks
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: page
  23. // in: query
  24. // description: page number of results to return (1-based)
  25. // type: integer
  26. // - name: limit
  27. // in: query
  28. // description: page size of results
  29. // type: integer
  30. // responses:
  31. // "200":
  32. // "$ref": "#/responses/CronList"
  33. // "403":
  34. // "$ref": "#/responses/forbidden"
  35. tasks := cron.ListTasks()
  36. count := len(tasks)
  37. listOpts := utils.GetListOptions(ctx)
  38. tasks = util.PaginateSlice(tasks, listOpts.Page, listOpts.PageSize).(cron.TaskTable)
  39. res := make([]structs.Cron, len(tasks))
  40. for i, task := range tasks {
  41. res[i] = structs.Cron{
  42. Name: task.Name,
  43. Schedule: task.Spec,
  44. Next: task.Next,
  45. Prev: task.Prev,
  46. ExecTimes: task.ExecTimes,
  47. }
  48. }
  49. ctx.SetTotalCountHeader(int64(count))
  50. ctx.JSON(http.StatusOK, res)
  51. }
  52. // PostCronTask api for getting cron tasks
  53. func PostCronTask(ctx *context.APIContext) {
  54. // swagger:operation POST /admin/cron/{task} admin adminCronRun
  55. // ---
  56. // summary: Run cron task
  57. // produces:
  58. // - application/json
  59. // parameters:
  60. // - name: task
  61. // in: path
  62. // description: task to run
  63. // type: string
  64. // required: true
  65. // responses:
  66. // "204":
  67. // "$ref": "#/responses/empty"
  68. // "404":
  69. // "$ref": "#/responses/notFound"
  70. task := cron.GetTask(ctx.Params(":task"))
  71. if task == nil {
  72. ctx.NotFound()
  73. return
  74. }
  75. task.Run()
  76. log.Trace("Cron Task %s started by admin(%s)", task.Name, ctx.User.Name)
  77. ctx.Status(http.StatusNoContent)
  78. }