summaryrefslogtreecommitdiffstats
path: root/jobs/testjob.go
blob: 59d5274e55d8eb7ba0c73f6bf9e9d37cef64e2ff (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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package jobs

import (
	"time"

	l4g "github.com/alecthomas/log4go"
	"github.com/mattermost/platform/store"
)

type TestJob struct {
	store store.Store

	name    string
	stop    chan bool
	stopped chan bool
}

func MakeTestJob(s store.Store, name string) *TestJob {
	return &TestJob{
		store:   s,
		name:    name,
		stop:    make(chan bool, 1),
		stopped: make(chan bool, 1),
	}
}

func (job *TestJob) Run() {
	l4g.Debug("Job %v: Started", job.name)

	running := true
	for running {
		l4g.Debug("Job %v: Tick", job.name)

		select {
		case <-job.stop:
			l4g.Debug("Job %v: Received stop signal", job.name)
			running = false
		case <-time.After(10 * time.Second):
			continue
		}
	}

	l4g.Debug("Job %v: Finished", job.name)
	job.stopped <- true
}

func (job *TestJob) Stop() {
	l4g.Debug("Job %v: Stopping", job.name)
	job.stop <- true
	<-job.stopped
}