github.com/mattermost/mattermost-plugin-api@v0.1.4/cluster/job_once_example_test.go (about) 1 package cluster 2 3 import ( 4 "log" 5 "time" 6 7 "github.com/mattermost/mattermost-server/v6/plugin" 8 ) 9 10 func HandleJobOnceCalls(key string, props any) { 11 if key == "the key i'm watching for" { 12 log.Println(props) 13 // Work to do only once per cluster 14 } 15 } 16 17 func ExampleJobOnceScheduler_ScheduleOnce() { 18 // Use p.API from your plugin instead. 19 pluginAPI := plugin.API(nil) 20 21 // Get the scheduler, which you can pass throughout the plugin... 22 scheduler := GetJobOnceScheduler(pluginAPI) 23 24 // Set the plugin's callback handler 25 _ = scheduler.SetCallback(HandleJobOnceCalls) 26 27 // Now start the scheduler, which starts the poller and schedules all waiting jobs. 28 _ = scheduler.Start() 29 30 // main thread... 31 32 // add a job 33 _, _ = scheduler.ScheduleOnce("the key i'm watching for", time.Now().Add(2*time.Hour), struct{ foo string }{"aasd"}) 34 35 // Maybe you want to check the scheduled jobs, or cancel them. This is completely optional--there 36 // is no need to cancel jobs, even if you are shutting down. Call Cancel only when you want to 37 // cancel a future job. Cancelling a job will prevent it from running in the future on this or 38 // any server. 39 jobs, _ := scheduler.ListScheduledJobs() 40 defer func() { 41 for _, j := range jobs { 42 scheduler.Cancel(j.Key) 43 } 44 }() 45 }