mirror of
https://github.com/dagucloud/dagu.git
synced 2026-12-06 13:36:49 +00:00
A schedule could not skip holidays or pick business days of the month, so payroll, settlement, and reporting jobs needed shell checks or hand-kept cron exceptions. A start schedule can name a calendar file, a list of holidays with optional weekdays, extra business days, and includes, and keep only chosen business days with business_days. The scheduler, catch-up, the upcoming runs preview, dagu ls -n, and the new dagu calendar fires and check commands all use the same rule, and DAGs reload when a calendar they use changes. Spec 084 defines the behavior.
161 lines
5.0 KiB
Go
161 lines
5.0 KiB
Go
// Copyright (C) 2026 Yota Hamada
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package test
|
|
|
|
import (
|
|
"context"
|
|
filedag "github.com/dagucloud/dagu/v2/internal/persis/file/dag"
|
|
"github.com/dagucloud/dagu/v2/internal/workspace"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/dagucloud/dagu/v2/internal/dispatch"
|
|
"github.com/dagucloud/dagu/v2/internal/persis"
|
|
"github.com/dagucloud/dagu/v2/internal/persis/file"
|
|
"github.com/dagucloud/dagu/v2/internal/persis/store"
|
|
"github.com/dagucloud/dagu/v2/internal/queue"
|
|
"github.com/dagucloud/dagu/v2/internal/runtime"
|
|
"github.com/dagucloud/dagu/v2/internal/schedulerstate"
|
|
"github.com/dagucloud/dagu/v2/internal/service/coordinator"
|
|
"github.com/dagucloud/dagu/v2/internal/service/scheduler"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Scheduler represents a test scheduler instance
|
|
type Scheduler struct {
|
|
Helper
|
|
EntryReader scheduler.EntryReader
|
|
QueueStore queue.QueueStore
|
|
CoordinatorCli dispatch.Dispatcher
|
|
// PauseStore backs the cluster-wide scheduler pause flag. Tests write to it
|
|
// the way the API process would.
|
|
PauseStore schedulerstate.PauseStore
|
|
}
|
|
|
|
// SetupScheduler creates a test scheduler instance with all dependencies
|
|
func SetupScheduler(t *testing.T, opts ...HelperOption) *Scheduler {
|
|
t.Helper()
|
|
|
|
// Create scheduler-specific options
|
|
schedulerOpts := make([]HelperOption, 0, len(opts)+1)
|
|
|
|
// Set up a test DAGs directory if not already specified
|
|
var hasDAGsDir bool
|
|
for _, opt := range opts {
|
|
schedulerOpts = append(schedulerOpts, opt)
|
|
// Check if DAGsDir option is already provided
|
|
// This is a simple check, in production code you might want a more robust solution
|
|
if opt != nil {
|
|
hasDAGsDir = true
|
|
}
|
|
}
|
|
|
|
// If no DAGsDir specified, use the testdata scheduler directory
|
|
if !hasDAGsDir {
|
|
testdataDir := TestdataPath(t, filepath.Join("scheduler"))
|
|
schedulerOpts = append(schedulerOpts, WithDAGsDir(testdataDir))
|
|
}
|
|
|
|
// Create the base helper
|
|
helper := Setup(t, schedulerOpts...)
|
|
|
|
// Update config for scheduler-specific settings
|
|
helper.Config.Scheduler.LockStaleThreshold = 30 * time.Second
|
|
helper.Config.Scheduler.LockRetryInterval = 50 * time.Millisecond
|
|
|
|
// Create additional stores needed for scheduler
|
|
ds, err := file.NewDAGRepository(helper.Config, file.WithDAGSkipExamples(true))
|
|
require.NoError(t, err)
|
|
dagRunRepository := file.NewDAGRunRepository(helper.Config)
|
|
ps := newProcRepository(helper.Config)
|
|
qs := store.NewQueueStore(helper.Backend.Collection(persis.CollectionQueue))
|
|
pauseStore := store.NewSchedulerPauseStore(helper.Backend.Collection(persis.CollectionSchedulerState))
|
|
|
|
// Create DAG run manager
|
|
drm := runtime.NewManager(dagRunRepository, ps, helper.Config)
|
|
|
|
// Create entry reader
|
|
coordinatorCli := coordinator.New(helper.ServiceRegistry, CoordinatorClientConfig(helper.Config.Paths.DataDir))
|
|
em := filedag.NewFileEntryReader(
|
|
helper.Config.Paths.DAGsDir,
|
|
ds,
|
|
helper.Config.DAGDiscovery.Recursive,
|
|
helper.Config.Paths.BaseConfig,
|
|
workspace.BaseConfigDir(helper.Config.Paths.DAGsDir),
|
|
filedag.WatchCalendars(helper.Config.Paths.CalendarsDir),
|
|
)
|
|
|
|
// Update helper with scheduler-specific stores
|
|
helper.DAGRepository = ds
|
|
helper.DAGRunRepository = dagRunRepository
|
|
helper.ProcRepository = ps
|
|
helper.DAGRunMgr = drm
|
|
|
|
sch := &Scheduler{
|
|
Helper: helper,
|
|
EntryReader: em,
|
|
QueueStore: qs,
|
|
CoordinatorCli: coordinatorCli,
|
|
PauseStore: pauseStore,
|
|
}
|
|
|
|
return sch
|
|
}
|
|
|
|
// NewSchedulerInstance creates a new scheduler instance for testing
|
|
func (s *Scheduler) NewSchedulerInstance(t *testing.T) (*scheduler.Scheduler, error) {
|
|
t.Helper()
|
|
|
|
return scheduler.New(s.Config, scheduler.Dependencies{
|
|
EntryReader: s.EntryReader,
|
|
DAGRunManager: s.DAGRunMgr,
|
|
DAGRepository: s.DAGRepository,
|
|
DAGRunRepository: s.DAGRunRepository,
|
|
QueueStore: s.QueueStore,
|
|
ProcRepository: s.ProcRepository,
|
|
ServiceRegistry: s.ServiceRegistry,
|
|
CoordinatorClient: s.CoordinatorCli,
|
|
SchedulerPauseStore: s.PauseStore,
|
|
})
|
|
}
|
|
|
|
// Start starts the scheduler instance
|
|
func (s *Scheduler) Start(t *testing.T, ctx context.Context) (*scheduler.Scheduler, chan error) {
|
|
t.Helper()
|
|
|
|
instance, err := s.NewSchedulerInstance(t)
|
|
require.NoError(t, err, "failed to create scheduler instance")
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
errCh <- instance.Start(ctx)
|
|
}()
|
|
|
|
var startErr error
|
|
var stopped bool
|
|
require.Eventually(t, func() bool {
|
|
select {
|
|
case startErr = <-errCh:
|
|
stopped = true
|
|
return true
|
|
default:
|
|
}
|
|
return instance.IsRunning()
|
|
}, 5*time.Second, 25*time.Millisecond, "scheduler should start")
|
|
require.False(t, stopped, "scheduler exited before it started: %v", startErr)
|
|
|
|
return instance, errCh
|
|
}
|
|
|
|
// StartAsync starts the scheduler instance asynchronously
|
|
func (s *Scheduler) StartAsync(t *testing.T) (*scheduler.Scheduler, chan error) {
|
|
return s.Start(t, s.Context)
|
|
}
|
|
|
|
// WithSchedulerTestDAGs creates a scheduler option for setting up test DAGs directory
|
|
func WithSchedulerTestDAGs(dagsDir string) HelperOption {
|
|
return WithDAGsDir(dagsDir)
|
|
}
|