gitea源码

cron.go 2.0KB

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