Skip to content

Commit 1bb2d26

Browse files
committed
use cluser api to schedule job
1 parent a046467 commit 1bb2d26

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

server/plugin/plugin.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/mattermost/mattermost/server/public/model"
1010
"github.com/mattermost/mattermost/server/public/plugin"
1111
"github.com/mattermost/mattermost/server/public/pluginapi"
12+
"github.com/mattermost/mattermost/server/public/pluginapi/cluster"
1213
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/poster"
1314
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/telemetry"
1415
"github.com/pkg/errors"
@@ -49,7 +50,7 @@ type Plugin struct {
4950

5051
oauthBroker *OAuthBroker
5152

52-
channelRefreshJob *time.Ticker
53+
channelRefreshJob *cluster.Job
5354
}
5455

5556
func (p *Plugin) ensurePluginAPIClient() {
@@ -164,13 +165,10 @@ func (p *Plugin) OnActivate() error {
164165

165166
// google drive watch api doesn't allow indefinite expiry of watch channels
166167
// so we need to refresh(close old channel and start new one) them before they get expired
167-
p.channelRefreshJob = time.NewTicker(12 * time.Hour)
168-
go func() {
169-
for range p.channelRefreshJob.C {
170-
p.refreshDriveWatchChannels()
171-
}
172-
}()
173-
168+
p.channelRefreshJob, err = cluster.Schedule(p.API, "refreshDriveWatchChannelsJob", cluster.MakeWaitForInterval(12*time.Hour), p.refreshDriveWatchChannels)
169+
if err != nil {
170+
return errors.Wrap(err, "failed to create a scheduled recurring job to refresh watch channels")
171+
}
174172
return nil
175173
}
176174

@@ -179,7 +177,9 @@ func (p *Plugin) OnDeactivate() error {
179177
if err := p.telemetryClient.Close(); err != nil {
180178
p.client.Log.Warn("Telemetry client failed to close", "error", err.Error())
181179
}
182-
p.channelRefreshJob.Stop()
180+
if err := p.channelRefreshJob.Close(); err != nil {
181+
p.client.Log.Warn("Channel refresh job failed to close", "error", err.Error())
182+
}
183183
return nil
184184
}
185185

0 commit comments

Comments
 (0)