diff --git a/crontab/v2/job.go b/crontab/v2/job.go new file mode 100644 index 00000000..97616d65 --- /dev/null +++ b/crontab/v2/job.go @@ -0,0 +1,6 @@ +package v2 //nolint:typecheck + +type Job interface { + Exp() string // Expression + Run() // Handler +} diff --git a/crontab/v2/null_jog.go b/crontab/v2/null_jog.go new file mode 100644 index 00000000..5ec3cc8e --- /dev/null +++ b/crontab/v2/null_jog.go @@ -0,0 +1 @@ +package v2 diff --git a/crontab/v2/server.go b/crontab/v2/server.go new file mode 100644 index 00000000..60bcfe8a --- /dev/null +++ b/crontab/v2/server.go @@ -0,0 +1,27 @@ +package v2 + +import ( + "context" + + "github.com/robfig/cron/v3" +) + +type Server struct { + *cron.Cron +} + +func NewServer(c *cron.Cron) *Server { + return &Server{ + Cron: c, + } +} + +func (s *Server) Start(context.Context) error { + s.Cron.Run() + return nil +} + +func (s *Server) Stop(context.Context) error { + s.Cron.Stop() + return nil +} diff --git a/crontab/v2/server_test.go b/crontab/v2/server_test.go new file mode 100644 index 00000000..b0d0adb9 --- /dev/null +++ b/crontab/v2/server_test.go @@ -0,0 +1,55 @@ +package v2 + +import ( + "context" + "testing" + "time" + + "github.com/robfig/cron/v3" +) + +type mockJob struct{} + +func RegisterMockJob(srv *Server) { + n := newMockJob() + _, _ = srv.AddJob(n.Exp(), n) +} + +func newMockJob() *mockJob { + return &mockJob{} +} + +func (j *mockJob) Exp() string { + return "* * * * * *" +} + +func (j *mockJob) Run() { + data <- "Hello World!" +} + +var ( + ctx = context.Background() + data = make(chan string, 1) +) + +func TestCrontab(t *testing.T) { + srv := NewServer(cron.New( + cron.WithSeconds(), + )) + + RegisterMockJob(srv) + + go srv.Start(ctx) //nolint:errcheck + defer srv.Stop(ctx) //nolint:errcheck + + ctx, cancel := context.WithTimeout(ctx, time.Second*2) + defer cancel() + + select { + case <-ctx.Done(): + t.Error("Crontab test timeout") + return + case msg := <-data: + t.Log(msg) + } +}