summaryrefslogtreecommitdiffstats
path: root/jobs/testscheduler.go
blob: d7345f67fb194f729539a2c70f0012fa7e37d72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package jobs

import (
	"time"

	l4g "github.com/alecthomas/log4go"
)

type TestScheduler struct {
	name    string
	jobType string
	stop    chan bool
	stopped chan bool
}

func MakeTestScheduler(name string, jobType string) *TestScheduler {
	return &TestScheduler{
		name:    name,
		jobType: jobType,
		stop:    make(chan bool, 1),
		stopped: make(chan bool, 1),
	}
}

func (scheduler *TestScheduler) Run() {
	l4g.Debug("Scheduler %v: Started", scheduler.name)

	defer func() {
		l4g.Debug("Scheduler %v: Finished", scheduler.name)
		scheduler.stopped <- true
	}()

	for {
		select {
		case <-scheduler.stop:
			l4g.Debug("Scheduler %v: Received stop signal", scheduler.name)
			return
		case <-time.After(86400 * time.Second):
			l4g.Debug("Scheduler: %v: Scheduling new job", scheduler.name)
			scheduler.AddJob()
		}
	}
}

func (scheduler *TestScheduler) AddJob() {
	if _, err := CreateJob(scheduler.jobType, nil); err != nil {
		l4g.Error("Scheduler %v: failed to create job: %v", scheduler.name, err)
	}
}

func (scheduler *TestScheduler) Stop() {
	l4g.Debug("Scheduler %v: Stopping", scheduler.name)
	scheduler.stop <- true
	<-scheduler.stopped
}