Skip to content

Commit

Permalink
Merge branch 'master' into daily-summary-batch
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmister committed Apr 12, 2020
2 parents 814de68 + d09debb commit 0461ccb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 114 deletions.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand Down
42 changes: 42 additions & 0 deletions server/jobs/daily_summary_job.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 5 additions & 2 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
97 changes: 0 additions & 97 deletions server/mscalendar/daily_summary_job.go

This file was deleted.

18 changes: 4 additions & 14 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0461ccb

Please sign in to comment.