Allow to run cron manually (#1338)

Closes #1154
This commit is contained in:
qwerty287
2022-10-26 01:23:28 +02:00
committed by GitHub
parent ed44c3b50f
commit de4e62cfcf
11 changed files with 76 additions and 16 deletions

View File

@@ -23,6 +23,7 @@ import (
"github.com/woodpecker-ci/woodpecker/server"
cronScheduler "github.com/woodpecker-ci/woodpecker/server/cron"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/pipeline"
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
"github.com/woodpecker-ci/woodpecker/server/store"
)
@@ -45,6 +46,37 @@ func GetCron(c *gin.Context) {
c.JSON(200, cron)
}
// RunCron starts a cron job now.
func RunCron(c *gin.Context) {
repo := session.Repo(c)
_store := store.FromContext(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
c.String(400, "Error parsing cron id. %s", err)
return
}
cron, err := _store.CronFind(repo, id)
if err != nil {
c.String(http.StatusNotFound, "Error getting cron %q. %s", id, err)
return
}
repo, newPipeline, err := cronScheduler.CreatePipeline(c, _store, server.Config.Services.Remote, cron)
if err != nil {
c.String(http.StatusInternalServerError, "Error creating pipeline for cron %q. %s", id, err)
return
}
pl, err := pipeline.Create(c, _store, repo, newPipeline)
if err != nil {
handlePipelineErr(c, err)
return
}
c.JSON(200, pl)
}
// PostCron persists the cron job to the database.
func PostCron(c *gin.Context) {
repo := session.Repo(c)

View File

@@ -21,6 +21,7 @@ package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -28,6 +29,7 @@ import (
"time"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/store/types"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
@@ -95,6 +97,10 @@ func GetPipelines(c *gin.Context) {
pipelines, err := store.FromContext(c).GetPipelineList(repo, page)
if err != nil {
if errors.Is(err, types.RecordNotExist) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.AbortWithStatus(http.StatusInternalServerError)
return
}