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