Skip to content

Commit

Permalink
EventManager: add timestamp and name to scheduled events
Browse files Browse the repository at this point in the history
Signed-off-by: Nicola Murino <[email protected]>
  • Loading branch information
drakkan committed Mar 4, 2025
1 parent da64cdf commit 5ca7070
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
14 changes: 12 additions & 2 deletions internal/common/eventmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,16 @@ func (j *eventCronJob) getTask(rule *dataprovider.EventRule) (dataprovider.Task,
return dataprovider.Task{}, nil
}

func (j *eventCronJob) getEventParams() EventParams {
return EventParams{
Event: "Schedule",
Name: j.ruleName,
Status: 1,
Timestamp: time.Now(),
updateStatusFromError: true,
}
}

func (j *eventCronJob) Run() {
eventManagerLog(logger.LevelDebug, "executing scheduled rule %q", j.ruleName)
rule, err := dataprovider.EventRuleExists(j.ruleName)
Expand Down Expand Up @@ -2881,9 +2891,9 @@ func (j *eventCronJob) Run() {
}
}(task.Name)

executeAsyncRulesActions([]dataprovider.EventRule{rule}, EventParams{Status: 1, updateStatusFromError: true})
executeAsyncRulesActions([]dataprovider.EventRule{rule}, j.getEventParams())
} else {
executeAsyncRulesActions([]dataprovider.EventRule{rule}, EventParams{Status: 1, updateStatusFromError: true})
executeAsyncRulesActions([]dataprovider.EventRule{rule}, j.getEventParams())
}
eventManagerLog(logger.LevelDebug, "execution for scheduled rule %q finished", j.ruleName)
}
Expand Down
41 changes: 33 additions & 8 deletions internal/common/eventmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,12 +1933,27 @@ func TestScheduledActions(t *testing.T) {
backupsPath := filepath.Join(os.TempDir(), "backups")
err := os.RemoveAll(backupsPath)
assert.NoError(t, err)
now := time.Now().UTC().Format(dateTimeMillisFormat)
// The backup action sets the home directory to the backup path.
expectedDirPath := filepath.Join(backupsPath, fmt.Sprintf("%s_%s_%s", now[0:4], now[5:7], now[8:10]))

action := &dataprovider.BaseEventAction{
Name: "action",
action1 := &dataprovider.BaseEventAction{
Name: "action1",
Type: dataprovider.ActionTypeBackup,
}
err = dataprovider.AddEventAction(action, "", "", "")
err = dataprovider.AddEventAction(action1, "", "", "")
assert.NoError(t, err)
action2 := &dataprovider.BaseEventAction{
Name: "action2",
Type: dataprovider.ActionTypeFilesystem,
Options: dataprovider.BaseEventActionOptions{
FsConfig: dataprovider.EventActionFilesystemConfig{
Type: dataprovider.FilesystemActionMkdirs,
MkDirs: []string{"{{Year}}_{{Month}}_{{Day}}"},
},
},
}
err = dataprovider.AddEventAction(action2, "", "", "")
assert.NoError(t, err)
rule := &dataprovider.EventRule{
Name: "rule",
Expand All @@ -1957,10 +1972,16 @@ func TestScheduledActions(t *testing.T) {
Actions: []dataprovider.EventAction{
{
BaseEventAction: dataprovider.BaseEventAction{
Name: action.Name,
Name: action1.Name,
},
Order: 1,
},
{
BaseEventAction: dataprovider.BaseEventAction{
Name: action2.Name,
},
Order: 2,
},
},
}

Expand All @@ -1975,26 +1996,30 @@ func TestScheduledActions(t *testing.T) {

job.Run()
assert.DirExists(t, backupsPath)
assert.DirExists(t, expectedDirPath)

action.Type = dataprovider.ActionTypeEmail
action.Options = dataprovider.BaseEventActionOptions{
action1.Type = dataprovider.ActionTypeEmail
action1.Options = dataprovider.BaseEventActionOptions{
EmailConfig: dataprovider.EventActionEmailConfig{
Recipients: []string{"[email protected]"},
Subject: "test with attachments",
Body: "body",
Attachments: []string{"/file1.txt"},
},
}
err = dataprovider.UpdateEventAction(action, "", "", "")
err = dataprovider.UpdateEventAction(action1, "", "", "")
assert.NoError(t, err)
job.Run() // action is not compatible with a scheduled rule

err = dataprovider.DeleteEventRule(rule.Name, "", "", "")
assert.NoError(t, err)
err = dataprovider.DeleteEventAction(action.Name, "", "", "")
err = dataprovider.DeleteEventAction(action1.Name, "", "", "")
assert.NoError(t, err)
err = dataprovider.DeleteEventAction(action2.Name, "", "", "")
assert.NoError(t, err)
err = os.RemoveAll(backupsPath)
assert.NoError(t, err)

stopEventScheduler()
}

Expand Down

0 comments on commit 5ca7070

Please sign in to comment.