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.

86 lines
2.0 KiB

  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/routers/api/v1/utils"
  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. listOpts := utils.GetListOptions(ctx)
  36. start, end := listOpts.GetStartEnd()
  37. if len(tasks) > listOpts.PageSize {
  38. tasks = tasks[start:end]
  39. }
  40. res := make([]structs.Cron, len(tasks))
  41. for i, task := range tasks {
  42. res[i] = structs.Cron{
  43. Name: task.Name,
  44. Schedule: task.Spec,
  45. Next: task.Next,
  46. Prev: task.Prev,
  47. ExecTimes: task.ExecTimes,
  48. }
  49. }
  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. }