forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cron | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sync" | ||
|
||
"github.com/robfig/cron/v3" | ||
) | ||
|
||
// cronFacade allows the client to operate using key rather than cron.EntryID, | ||
// as well as providing sync guarantees | ||
type cronFacade struct { | ||
mu sync.Mutex | ||
cron *cron.Cron | ||
entryIDs map[string]cron.EntryID | ||
} | ||
|
||
func newCronFacade() *cronFacade { | ||
return &cronFacade{ | ||
cron: cron.New(), | ||
entryIDs: make(map[string]cron.EntryID), | ||
} | ||
} | ||
|
||
func (f *cronFacade) Start() { | ||
f.cron.Start() | ||
} | ||
|
||
func (f *cronFacade) Stop() { | ||
f.cron.Stop() | ||
} | ||
|
||
func (f *cronFacade) Delete(key string) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
entryID, ok := f.entryIDs[key] | ||
if !ok { | ||
return | ||
} | ||
f.cron.Remove(entryID) | ||
delete(f.entryIDs, key) | ||
} | ||
|
||
func (f *cronFacade) AddJob(key, schedule string, cwoc *cronWfOperationCtx) error { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
entryID, err := f.cron.AddJob(schedule, cwoc) | ||
if err != nil { | ||
return err | ||
} | ||
f.entryIDs[key] = entryID | ||
return nil | ||
} | ||
|
||
func (f *cronFacade) Load(key string) (*cronWfOperationCtx, error) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
entryID, ok := f.entryIDs[key] | ||
if !ok { | ||
return nil, fmt.Errorf("entry ID for %s not found", key) | ||
} | ||
entry := f.cron.Entry(entryID).Job | ||
cwoc, ok := entry.(*cronWfOperationCtx) | ||
if !ok { | ||
return nil, fmt.Errorf("job entry ID for %s was not a *cronWfOperationCtx, was %v", key, reflect.TypeOf(entry)) | ||
} | ||
return cwoc, nil | ||
} |