mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 23:00:55 +03:00
EventManager: add timestamp and name to scheduled event parameters
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -2842,6 +2842,16 @@ func (j *eventCronJob) getTask(rule *dataprovider.EventRule) (dataprovider.Task,
|
|||||||
return dataprovider.Task{}, nil
|
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() {
|
func (j *eventCronJob) Run() {
|
||||||
eventManagerLog(logger.LevelDebug, "executing scheduled rule %q", j.ruleName)
|
eventManagerLog(logger.LevelDebug, "executing scheduled rule %q", j.ruleName)
|
||||||
rule, err := dataprovider.EventRuleExists(j.ruleName)
|
rule, err := dataprovider.EventRuleExists(j.ruleName)
|
||||||
@@ -2892,9 +2902,9 @@ func (j *eventCronJob) Run() {
|
|||||||
}
|
}
|
||||||
}(task.Name)
|
}(task.Name)
|
||||||
|
|
||||||
executeAsyncRulesActions([]dataprovider.EventRule{rule}, EventParams{Status: 1, updateStatusFromError: true})
|
executeAsyncRulesActions([]dataprovider.EventRule{rule}, j.getEventParams())
|
||||||
} else {
|
} 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)
|
eventManagerLog(logger.LevelDebug, "execution for scheduled rule %q finished", j.ruleName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1954,12 +1954,27 @@ func TestScheduledActions(t *testing.T) {
|
|||||||
backupsPath := filepath.Join(os.TempDir(), "backups")
|
backupsPath := filepath.Join(os.TempDir(), "backups")
|
||||||
err := os.RemoveAll(backupsPath)
|
err := os.RemoveAll(backupsPath)
|
||||||
assert.NoError(t, err)
|
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{
|
action1 := &dataprovider.BaseEventAction{
|
||||||
Name: "action",
|
Name: "action1",
|
||||||
Type: dataprovider.ActionTypeBackup,
|
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)
|
assert.NoError(t, err)
|
||||||
rule := &dataprovider.EventRule{
|
rule := &dataprovider.EventRule{
|
||||||
Name: "rule",
|
Name: "rule",
|
||||||
@@ -1978,10 +1993,16 @@ func TestScheduledActions(t *testing.T) {
|
|||||||
Actions: []dataprovider.EventAction{
|
Actions: []dataprovider.EventAction{
|
||||||
{
|
{
|
||||||
BaseEventAction: dataprovider.BaseEventAction{
|
BaseEventAction: dataprovider.BaseEventAction{
|
||||||
Name: action.Name,
|
Name: action1.Name,
|
||||||
},
|
},
|
||||||
Order: 1,
|
Order: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
BaseEventAction: dataprovider.BaseEventAction{
|
||||||
|
Name: action2.Name,
|
||||||
|
},
|
||||||
|
Order: 2,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1996,9 +2017,10 @@ func TestScheduledActions(t *testing.T) {
|
|||||||
|
|
||||||
job.Run()
|
job.Run()
|
||||||
assert.DirExists(t, backupsPath)
|
assert.DirExists(t, backupsPath)
|
||||||
|
assert.DirExists(t, expectedDirPath)
|
||||||
|
|
||||||
action.Type = dataprovider.ActionTypeEmail
|
action1.Type = dataprovider.ActionTypeEmail
|
||||||
action.Options = dataprovider.BaseEventActionOptions{
|
action1.Options = dataprovider.BaseEventActionOptions{
|
||||||
EmailConfig: dataprovider.EventActionEmailConfig{
|
EmailConfig: dataprovider.EventActionEmailConfig{
|
||||||
Recipients: []string{"example@example.com"},
|
Recipients: []string{"example@example.com"},
|
||||||
Subject: "test with attachments",
|
Subject: "test with attachments",
|
||||||
@@ -2006,16 +2028,19 @@ func TestScheduledActions(t *testing.T) {
|
|||||||
Attachments: []string{"/file1.txt"},
|
Attachments: []string{"/file1.txt"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err = dataprovider.UpdateEventAction(action, "", "", "")
|
err = dataprovider.UpdateEventAction(action1, "", "", "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
job.Run() // action is not compatible with a scheduled rule
|
job.Run() // action is not compatible with a scheduled rule
|
||||||
|
|
||||||
err = dataprovider.DeleteEventRule(rule.Name, "", "", "")
|
err = dataprovider.DeleteEventRule(rule.Name, "", "", "")
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
err = os.RemoveAll(backupsPath)
|
err = os.RemoveAll(backupsPath)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
stopEventScheduler()
|
stopEventScheduler()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user