File size: 1,842 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package app

import (
	"context"
	"database/sql"

	"github.com/target/goalert/engine"
	"github.com/target/goalert/notification"

	"github.com/pkg/errors"
)

func (app *App) initEngine(ctx context.Context) error {
	var regionIndex int
	err := app.db.QueryRowContext(ctx, `SELECT id FROM region_ids WHERE name = $1`, app.cfg.RegionName).Scan(&regionIndex)
	if errors.Is(err, sql.ErrNoRows) {
		// doesn't exist, try to create
		_, err = app.db.ExecContext(ctx, `
		INSERT INTO region_ids (name) VALUES ($1) 
		ON CONFLICT DO NOTHING`, app.cfg.RegionName)
		if err != nil {
			return errors.Wrap(err, "insert region")
		}

		err = app.db.QueryRowContext(ctx, `SELECT id FROM region_ids WHERE name = $1`, app.cfg.RegionName).Scan(&regionIndex)
	}
	if err != nil {
		return errors.Wrap(err, "get region index")
	}
	app.notificationManager = notification.NewManager(app.DestRegistry)
	app.Engine, err = engine.NewEngine(ctx, app.db, &engine.Config{
		EventBus:            app.EventBus,
		AlertStore:          app.AlertStore,
		AlertLogStore:       app.AlertLogStore,
		ContactMethodStore:  app.ContactMethodStore,
		NotificationManager: app.notificationManager,
		UserStore:           app.UserStore,
		NotificationStore:   app.NotificationStore,
		NCStore:             app.NCStore,
		OnCallStore:         app.OnCallStore,
		ScheduleStore:       app.ScheduleStore,
		AuthLinkStore:       app.AuthLinkStore,
		SlackStore:          app.slackChan,
		DestRegistry:        app.DestRegistry,

		ConfigSource: app.ConfigStore,

		CycleTime: app.cfg.EngineCycleTime,

		MaxMessages: 50,

		DisableCycle: app.cfg.APIOnly,
		LogCycles:    app.cfg.LogEngine,
		River:        app.River,
		RiverDBSQL:   app.RiverDBSQL,
		RiverWorkers: app.RiverWorkers,
		Logger:       app.Logger,
	})
	if err != nil {
		return errors.Wrap(err, "init engine")
	}

	return nil
}