summaryrefslogtreecommitdiff
path: root/internal/server/continuous.go
blob: 90e5f8aef83efa7d633983f9f7652428f218eaa3 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package server

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/mimecast/dtail/internal/clients"
	"github.com/mimecast/dtail/internal/config"
	"github.com/mimecast/dtail/internal/io/dlog"
	"github.com/mimecast/dtail/internal/omode"
	gossh "golang.org/x/crypto/ssh"
)

type continuous struct {
	cfg config.RuntimeConfig
}

func newContinuous(cfg config.RuntimeConfig) *continuous {
	return &continuous{cfg: cfg}
}

func (c *continuous) start(ctx context.Context) {
	dlog.Server.Info("Starting continuous job runner after 2s")
	time.Sleep(time.Second * 2)
	c.runJobs(ctx)
}

func (c *continuous) runJobs(ctx context.Context) {
	for _, job := range c.cfg.Server.Continuous {
		if !job.Enable {
			dlog.Server.Debug(job.Name, "Not running job as not enabled")
			continue
		}
		go func(job config.Continuous) {
			c.runJob(ctx, job)
			for {
				select {
				// Retry after a minute
				case <-time.After(time.Minute):
					c.runJob(ctx, job)
				case <-ctx.Done():
					return
				}
			}
		}(job)
	}
}

func (c *continuous) runJob(ctx context.Context, job config.Continuous) {
	dlog.Server.Debug(job.Name, "Processing job")

	files := fillDates(job.Files)
	outfile := fillDates(job.Outfile)
	servers := strings.Join(job.Servers, ",")
	if servers == "" {
		servers = c.cfg.Server.SSHBindAddress
	}

	args := config.Args{
		ConnectionsPerCPU: config.DefaultConnectionsPerCPU,
		Discovery:         job.Discovery,
		ServersStr:        servers,
		What:              files,
		Mode:              omode.TailClient,
		UserName:          config.ContinuousUser,
	}

	args.SSHAuthMethods = append(args.SSHAuthMethods, gossh.Password(job.Name))
	args.QueryStr = fmt.Sprintf("%s outfile %s", job.Query, outfile)
	client, err := clients.NewMaprClient(args, clients.NonCumulativeMode)
	if err != nil {
		dlog.Server.Error(fmt.Sprintf("Unable to create job %s", job.Name), err)
		return
	}

	jobCtx, cancel := context.WithCancel(ctx)
	defer cancel()
	if job.RestartOnDayChange {
		go func() {
			if c.waitForDayChange(ctx) {
				dlog.Server.Info(fmt.Sprintf("Canceling job %s due to day change", job.Name))
				cancel()
			}
		}()
	}

	dlog.Server.Info(fmt.Sprintf("Starting job %s", job.Name))
	status := client.Start(jobCtx, make(chan string))
	logMessage := fmt.Sprintf("Job exited with status %d", status)
	if status != 0 {
		dlog.Server.Warn(logMessage)
		return
	}
	dlog.Server.Info(logMessage)
}

func (c *continuous) waitForDayChange(ctx context.Context) bool {
	startTime := time.Now()
	for {
		select {
		case <-time.After(time.Second):
			if time.Now().Day() != startTime.Day() {
				return true
			}
		case <-ctx.Done():
			return false
		}
	}
}