code.gitea.io/gitea@v1.21.7/routers/api/v1/admin/cron.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package admin
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"code.gitea.io/gitea/modules/context"
    10  	"code.gitea.io/gitea/modules/log"
    11  	"code.gitea.io/gitea/modules/structs"
    12  	"code.gitea.io/gitea/modules/util"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  	"code.gitea.io/gitea/services/cron"
    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  	count := len(tasks)
    40  
    41  	listOpts := utils.GetListOptions(ctx)
    42  	tasks = util.PaginateSlice(tasks, listOpts.Page, listOpts.PageSize).(cron.TaskTable)
    43  
    44  	res := make([]structs.Cron, len(tasks))
    45  	for i, task := range tasks {
    46  		res[i] = structs.Cron{
    47  			Name:      task.Name,
    48  			Schedule:  task.Spec,
    49  			Next:      task.Next,
    50  			Prev:      task.Prev,
    51  			ExecTimes: task.ExecTimes,
    52  		}
    53  	}
    54  
    55  	ctx.SetTotalCountHeader(int64(count))
    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.Doer.Name)
    84  
    85  	ctx.Status(http.StatusNoContent)
    86  }