1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00

added cron.Total method

This commit is contained in:
Gani Georgiev 2023-07-08 13:51:00 +03:00
parent 7bcd00a87e
commit 5e37c90dde
2 changed files with 34 additions and 2 deletions

View File

@ -117,6 +117,14 @@ func (c *Cron) RemoveAll() {
c.jobs = map[string]*job{} c.jobs = map[string]*job{}
} }
// Total returns the current total number of registered cron jobs.
func (c *Cron) Total() int {
c.RLock()
defer c.RUnlock()
return len(c.jobs)
}
// Stop stops the current cron ticker (if not already). // Stop stops the current cron ticker (if not already).
// //
// You can resume the ticker by calling Start(). // You can resume the ticker by calling Start().
@ -139,9 +147,8 @@ func (c *Cron) Start() {
c.Stop() c.Stop()
c.Lock() c.Lock()
defer c.Unlock()
c.ticker = time.NewTicker(c.interval) c.ticker = time.NewTicker(c.interval)
c.Unlock()
go func() { go func() {
for t := range c.ticker.C { for t := range c.ticker.C {

View File

@ -169,6 +169,31 @@ func TestCronRemoveAll(t *testing.T) {
} }
} }
func TestCronTotal(t *testing.T) {
c := New()
if v := c.Total(); v != 0 {
t.Fatalf("Expected 0 jobs, got %v", v)
}
if err := c.Add("test1", "* * * * *", func() {}); err != nil {
t.Fatal(err)
}
if err := c.Add("test2", "* * * * *", func() {}); err != nil {
t.Fatal(err)
}
// overwrite
if err := c.Add("test1", "* * * * *", func() {}); err != nil {
t.Fatal(err)
}
if v := c.Total(); v != 2 {
t.Fatalf("Expected 2 jobs, got %v", v)
}
}
func TestCronStartStop(t *testing.T) { func TestCronStartStop(t *testing.T) {
c := New() c := New()