2022-08-31 22:36:32 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robfig/cron"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-08-31 22:36:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-05-13 20:58:21 +00:00
|
|
|
// Specifies the interval woodpecker checks for new crons to exec.
|
2022-08-31 22:36:32 +00:00
|
|
|
checkTime = 10 * time.Second
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Specifies the batch size of crons to retrieve per check from database.
|
2022-08-31 22:36:32 +00:00
|
|
|
checkItems = 10
|
|
|
|
)
|
|
|
|
|
2024-07-13 23:46:01 +00:00
|
|
|
// Run starts the cron scheduler loop.
|
|
|
|
func Run(ctx context.Context, store store.Store) error {
|
2022-08-31 22:36:32 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(checkTime):
|
|
|
|
go func() {
|
|
|
|
now := time.Now()
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Trace().Msg("cron: fetch next crons")
|
2022-08-31 22:36:32 +00:00
|
|
|
|
|
|
|
crons, err := store.CronListNextExecute(now.Unix(), checkItems)
|
|
|
|
if err != nil {
|
2022-11-09 07:12:17 +00:00
|
|
|
log.Error().Err(err).Int64("now", now.Unix()).Msg("obtain cron list")
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cron := range crons {
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := runCron(ctx, store, cron, now); err != nil {
|
2022-08-31 22:36:32 +00:00
|
|
|
log.Error().Err(err).Int64("cronID", cron.ID).Msg("run cron failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// CalcNewNext parses a cron string and calculates the next exec time based on it.
|
2022-08-31 22:36:32 +00:00
|
|
|
func CalcNewNext(schedule string, now time.Time) (time.Time, error) {
|
|
|
|
// remove local timezone
|
|
|
|
now = now.UTC()
|
|
|
|
|
|
|
|
// TODO: allow the users / the admin to set a specific timezone
|
|
|
|
|
|
|
|
c, err := cron.Parse(schedule)
|
|
|
|
if err != nil {
|
2023-02-01 23:08:02 +00:00
|
|
|
return time.Time{}, fmt.Errorf("cron parse schedule: %w", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
return c.Next(now), nil
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func runCron(ctx context.Context, store store.Store, cron *model.Cron, now time.Time) error {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Trace().Msgf("cron: run id[%d]", cron.ID)
|
2022-08-31 22:36:32 +00:00
|
|
|
|
|
|
|
newNext, err := CalcNewNext(cron.Schedule, now)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to get lock on cron
|
|
|
|
gotLock, err := store.CronGetLock(cron, newNext.Unix())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !gotLock {
|
|
|
|
// another go routine caught it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
repo, newPipeline, err := CreatePipeline(ctx, store, cron)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-25 23:23:28 +00:00
|
|
|
_, err = pipeline.Create(ctx, store, repo, newPipeline)
|
2022-08-31 22:36:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func CreatePipeline(ctx context.Context, store store.Store, cron *model.Cron) (*model.Repo, *model.Pipeline, error) {
|
2022-08-31 22:36:32 +00:00
|
|
|
repo, err := store.GetRepo(cron.RepoID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:36:32 +00:00
|
|
|
if cron.Branch == "" {
|
|
|
|
// fallback to the repos default branch
|
|
|
|
cron.Branch = repo.Branch
|
|
|
|
}
|
|
|
|
|
|
|
|
creator, err := store.GetUser(cron.CreatorID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-08 12:05:06 +00:00
|
|
|
// If the forge has a refresh token, the current access token
|
2022-11-19 20:47:47 +00:00
|
|
|
// may be stale. Therefore, we should refresh prior to dispatching
|
|
|
|
// the pipeline.
|
2024-04-16 06:04:55 +00:00
|
|
|
forge.Refresh(ctx, _forge, store, creator)
|
2022-11-19 20:47:47 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
commit, err := _forge.BranchHead(ctx, creator, repo, cron.Branch)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return repo, &model.Pipeline{
|
2022-08-31 22:36:32 +00:00
|
|
|
Event: model.EventCron,
|
2024-02-11 09:44:50 +00:00
|
|
|
Commit: commit.SHA,
|
2022-08-31 22:36:32 +00:00
|
|
|
Ref: "refs/heads/" + cron.Branch,
|
|
|
|
Branch: cron.Branch,
|
|
|
|
Message: cron.Name,
|
|
|
|
Timestamp: cron.NextExec,
|
|
|
|
Sender: cron.Name,
|
2024-02-11 09:44:50 +00:00
|
|
|
ForgeURL: commit.ForgeURL,
|
2022-08-31 22:36:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|