From d09debb838500150db8443316eb049d3f2f3a262 Mon Sep 17 00:00:00 2001 From: Michael Kochell <6913320+mickmister@users.noreply.github.com> Date: Sat, 11 Apr 2020 04:25:43 -0400 Subject: [PATCH] Use cluster.Job for daily summary job (#74) disable job by default Co-authored-by: Lev <1187448+levb@users.noreply.github.com> --- plugin.json | 2 +- server/jobs/daily_summary_job.go | 48 +++++++++++++ server/mscalendar/daily_summary.go | 7 +- server/mscalendar/daily_summary_job.go | 97 -------------------------- server/plugin/plugin.go | 18 ++--- 5 files changed, 58 insertions(+), 114 deletions(-) create mode 100644 server/jobs/daily_summary_job.go delete mode 100644 server/mscalendar/daily_summary_job.go 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..5c39c25a --- /dev/null +++ b/server/jobs/daily_summary_job.go @@ -0,0 +1,48 @@ +// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. +// See License for license information. + +package jobs + +import ( + "github.com/mattermost/mattermost-plugin-mscalendar/server/config" + "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) { + mscal := mscalendar.New(env, "") + _, err := mscal.GetRemoteUser(env.BotUserID) + if err != nil { + env.Logger.Errorf("Please connect bot user using `/%s connect_bot`, in order to run the daily summary job.", config.CommandTrigger) + return + } + + env.Logger.Debugf("Daily summary job beginning") + + err = mscal.ProcessAllDailySummary() + 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 2988b3f8..b54dcff2 100644 --- a/server/mscalendar/daily_summary.go +++ b/server/mscalendar/daily_summary.go @@ -14,6 +14,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 { @@ -45,8 +48,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 156d3ae7..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() - 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