Skip to content

Commit 2ae8c7a

Browse files
Add cron running API (#12421)
* Add cron running API Signed-off-by: Andrew Thornton <[email protected]> * Apply suggestions from code review * placate-swagger Signed-off-by: Andrew Thornton <[email protected]> * return not found Signed-off-by: Andrew Thornton <[email protected]> * Apply suggestions from code review Co-authored-by: techknowlogick <[email protected]>
1 parent ee04731 commit 2ae8c7a

File tree

6 files changed

+232
-0
lines changed

6 files changed

+232
-0
lines changed

models/list_options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func (opts ListOptions) setEnginePagination(e Engine) Engine {
3737
return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
3838
}
3939

40+
// GetStartEnd returns the start and end of the ListOptions
41+
func (opts ListOptions) GetStartEnd() (start, end int) {
42+
opts.setDefaultValues()
43+
start = (opts.Page - 1) * opts.PageSize
44+
end = start + opts.Page
45+
return
46+
}
47+
4048
func (opts ListOptions) setDefaultValues() {
4149
if opts.PageSize <= 0 {
4250
opts.PageSize = setting.API.DefaultPagingNum

modules/structs/cron.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 structs
6+
7+
import "time"
8+
9+
// Cron represents a Cron task
10+
type Cron struct {
11+
Name string `json:"name"`
12+
Schedule string `json:"schedule"`
13+
Next time.Time `json:"next"`
14+
Prev time.Time `json:"prev"`
15+
ExecTimes int64 `json:"exec_times"`
16+
}

routers/api/v1/admin/cron.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

routers/api/v1/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ func RegisterRoutes(m *macaron.Macaron) {
934934
})
935935

936936
m.Group("/admin", func() {
937+
m.Group("/cron", func() {
938+
m.Get("", admin.ListCronTasks)
939+
m.Post("/:task", admin.PostCronTask)
940+
})
937941
m.Get("/orgs", admin.GetAllOrgs)
938942
m.Group("/users", func() {
939943
m.Get("", admin.GetAllUsers)

routers/api/v1/swagger/cron.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 swagger
6+
7+
import (
8+
api "code.gitea.io/gitea/modules/structs"
9+
)
10+
11+
// CronList
12+
// swagger:response CronList
13+
type swaggerResponseCronList struct {
14+
// in:body
15+
Body []api.Cron `json:"body"`
16+
}

templates/swagger/v1_json.tmpl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,69 @@
2323
},
2424
"basePath": "{{AppSubUrl}}/api/v1",
2525
"paths": {
26+
"/admin/cron": {
27+
"get": {
28+
"produces": [
29+
"application/json"
30+
],
31+
"tags": [
32+
"admin"
33+
],
34+
"summary": "List cron tasks",
35+
"operationId": "adminCronList",
36+
"parameters": [
37+
{
38+
"type": "integer",
39+
"description": "page number of results to return (1-based)",
40+
"name": "page",
41+
"in": "query"
42+
},
43+
{
44+
"type": "integer",
45+
"description": "page size of results",
46+
"name": "limit",
47+
"in": "query"
48+
}
49+
],
50+
"responses": {
51+
"200": {
52+
"$ref": "#/responses/CronList"
53+
},
54+
"403": {
55+
"$ref": "#/responses/forbidden"
56+
}
57+
}
58+
}
59+
},
60+
"/admin/cron/{task}": {
61+
"post": {
62+
"produces": [
63+
"application/json"
64+
],
65+
"tags": [
66+
"admin"
67+
],
68+
"summary": "Run cron task",
69+
"operationId": "adminCronRun",
70+
"parameters": [
71+
{
72+
"type": "string",
73+
"description": "task to run",
74+
"name": "task",
75+
"in": "path",
76+
"required": true
77+
}
78+
],
79+
"responses": {
80+
"204": {
81+
"$ref": "#/responses/empty"
82+
},
83+
"404": {
84+
"$ref": "#/responses/notFound"
85+
}
86+
}
87+
}
88+
},
2689
"/admin/orgs": {
2790
"get": {
2891
"produces": [
@@ -11931,6 +11994,36 @@
1193111994
},
1193211995
"x-go-package": "code.gitea.io/gitea/modules/structs"
1193311996
},
11997+
"Cron": {
11998+
"description": "Cron represents a Cron task",
11999+
"type": "object",
12000+
"properties": {
12001+
"exec_times": {
12002+
"type": "integer",
12003+
"format": "int64",
12004+
"x-go-name": "ExecTimes"
12005+
},
12006+
"name": {
12007+
"type": "string",
12008+
"x-go-name": "Name"
12009+
},
12010+
"next": {
12011+
"type": "string",
12012+
"format": "date-time",
12013+
"x-go-name": "Next"
12014+
},
12015+
"prev": {
12016+
"type": "string",
12017+
"format": "date-time",
12018+
"x-go-name": "Prev"
12019+
},
12020+
"schedule": {
12021+
"type": "string",
12022+
"x-go-name": "Schedule"
12023+
}
12024+
},
12025+
"x-go-package": "code.gitea.io/gitea/modules/structs"
12026+
},
1193412027
"DeleteEmailOption": {
1193512028
"description": "DeleteEmailOption options when deleting email addresses",
1193612029
"type": "object",
@@ -15027,6 +15120,15 @@
1502715120
"$ref": "#/definitions/ContentsResponse"
1502815121
}
1502915122
},
15123+
"CronList": {
15124+
"description": "CronList",
15125+
"schema": {
15126+
"type": "array",
15127+
"items": {
15128+
"$ref": "#/definitions/Cron"
15129+
}
15130+
}
15131+
},
1503015132
"DeployKey": {
1503115133
"description": "DeployKey",
1503215134
"schema": {

0 commit comments

Comments
 (0)