diff --git a/plugin.json b/plugin.json index 11088c99..b964407f 100644 --- a/plugin.json +++ b/plugin.json @@ -92,7 +92,7 @@ "display_name": "Enable Daily Summary", "type": "bool", "help_text": "When enabled, Mattermost users will a receive a daily summary of their scheduled events in Microsoft Calendar. Users can choose when the summary will be sent to them.", - "default": true + "default": false } ] } diff --git a/server/jobs/daily_summary_job.go b/server/jobs/daily_summary_job.go new file mode 100644 index 00000000..1e5bd418 --- /dev/null +++ b/server/jobs/daily_summary_job.go @@ -0,0 +1,42 @@ +// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. +// See License for license information. + +package jobs + +import ( + "time" + + "github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar" +) + +// Unique id for the daily summary job +const dailySummaryJobID = "daily_summary" + +const dailySummaryJobInterval = mscalendar.DailySummaryJobInterval + +// NewDailySummaryJob creates a RegisteredJob with the parameters specific to the DailySummaryJob +func NewDailySummaryJob() RegisteredJob { + return RegisteredJob{ + id: dailySummaryJobID, + interval: dailySummaryJobInterval, + work: runDailySummaryJob, + isEnabledByConfig: isDailySummaryJobEnabled, + } +} + +// runDailySummaryJob delivers the daily calendar summary to all users who have their settings configured to receive it now +func runDailySummaryJob(env mscalendar.Env) { + env.Logger.Debugf("Daily summary job beginning") + + err := mscalendar.New(env, "").ProcessAllDailySummary(time.Now()) + if err != nil { + env.Logger.Errorf("Error during daily summary job", "error", err.Error()) + } + + env.Logger.Debugf("Daily summary job finished") +} + +// isDailySummaryJobEnabled uses current config to determine whether the job is enabled. +func isDailySummaryJobEnabled(env mscalendar.Env) bool { + return env.EnableDailySummary +} diff --git a/server/mscalendar/daily_summary.go b/server/mscalendar/daily_summary.go index 05cd99b0..abe9720a 100644 --- a/server/mscalendar/daily_summary.go +++ b/server/mscalendar/daily_summary.go @@ -15,6 +15,9 @@ import ( const dailySummaryTimeWindow = time.Minute * 2 +// Run daily summary job every 15 minutes +const DailySummaryJobInterval = 15 * time.Minute + var timeNowFunc = time.Now type DailySummary interface { @@ -46,8 +49,8 @@ func (m *mscalendar) SetDailySummaryPostTime(user *User, timeStr string) (*store return nil, errors.New("Invalid time value: " + timeStr) } - if t.Minute()%int(dailySummaryJobInterval/time.Minute) != 0 { - return nil, errors.Errorf("Time must be a multiple of %d minutes.", dailySummaryJobInterval/time.Minute) + if t.Minute()%int(DailySummaryJobInterval/time.Minute) != 0 { + return nil, errors.Errorf("Time must be a multiple of %d minutes.", DailySummaryJobInterval/time.Minute) } timezone, err := m.GetTimezone(user) diff --git a/server/mscalendar/daily_summary_job.go b/server/mscalendar/daily_summary_job.go deleted file mode 100644 index ee6a8743..00000000 --- a/server/mscalendar/daily_summary_job.go +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. -// See License for license information. - -package mscalendar - -import ( - "sync" - "time" - - "github.com/mattermost/mattermost-plugin-mscalendar/server/config" -) - -const dailySummaryJobInterval = 15 * time.Minute - -type DailySummaryJob struct { - Env - - cancel chan struct{} - cancelled chan struct{} - cancelOnce sync.Once -} - -func NewDailySummaryJob(env Env) *DailySummaryJob { - return &DailySummaryJob{ - cancel: make(chan struct{}), - cancelled: make(chan struct{}), - Env: env, - } -} - -func (j *DailySummaryJob) Start() { - go func() { - defer close(j.cancelled) - mscal := New(j.Env, "") - _, err := mscal.GetRemoteUser(j.Env.BotUserID) - if err != nil { - j.Logger.Errorf("Please connect bot user using `/%s connect_bot`, then re-enable the daily summary job.", config.CommandTrigger) - j.Cancel() - return - } - - firstRun := j.timerUntilFirstRun() - - var ticker *time.Ticker - select { - case <-firstRun.C: - ticker = time.NewTicker(dailySummaryJobInterval) - defer func() { - ticker.Stop() - }() - j.work() - case <-j.cancel: - return - } - - for { - select { - case <-ticker.C: - j.work() - case <-j.cancel: - return - } - } - }() -} - -func (job *DailySummaryJob) Cancel() { - job.cancelOnce.Do(func() { - close(job.cancel) - }) - <-job.cancelled -} - -func (j *DailySummaryJob) work() { - j.Logger.Debugf("Daily summary job beginning") - - err := New(j.Env, "").ProcessAllDailySummary(time.Now()) - if err != nil { - j.Logger.Errorf("Error during daily summary job", "error", err.Error()) - } - - j.Logger.Debugf("Daily summary job finished") -} - -// timeUntilFirstRun uses a job's interval to compute the time duration until the initial run. -func (j *DailySummaryJob) timerUntilFirstRun() *time.Timer { - now := timeNowFunc() - interval := dailySummaryJobInterval - - leftBound := now.Truncate(interval) - target := leftBound.Add(interval) - - j.Logger.Debugf("Waiting until %s to run daily summary job", target.Format(time.Kitchen)) - - diff := target.Sub(now) - return time.NewTimer(diff) -} diff --git a/server/plugin/plugin.go b/server/plugin/plugin.go index add288a0..58703e80 100644 --- a/server/plugin/plugin.go +++ b/server/plugin/plugin.go @@ -34,7 +34,6 @@ import ( type Env struct { mscalendar.Env bot bot.Bot - dailySummaryJob *mscalendar.DailySummaryJob jobManager *jobs.JobManager notificationProcessor mscalendar.NotificationProcessor httpHandler *httputils.Handler @@ -149,25 +148,16 @@ func (p *Plugin) OnConfigurationChange() (err error) { if err != nil { e.Logger.Errorf(err.Error()) } + err = e.jobManager.AddJob(jobs.NewDailySummaryJob()) + if err != nil { + e.Logger.Errorf(err.Error()) + } } err := e.jobManager.OnConfigurationChange(e.Env) if err != nil { e.Logger.Errorf(err.Error()) } - { - if e.EnableDailySummary && e.dailySummaryJob == nil { - e.Logger.Debugf("Enabling daily summary job") - e.dailySummaryJob = mscalendar.NewDailySummaryJob(e.Env) - go e.dailySummaryJob.Start() - } - - if !e.EnableDailySummary && e.dailySummaryJob != nil { - e.Logger.Debugf("Disabling daily summary job") - e.dailySummaryJob.Cancel() - e.dailySummaryJob = nil - } - } }) return nil