aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/pkg/workflow/service.go
blob: 4ec7eb5cf1aa5f66c969d6879ea957599946bc56 (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
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package workflow

import (
	"sync"
	"time"
)

// Service is the interface for starting and managing the workflows that process individual patch series.
// The workflow includes steps like building base/patched kernel, dong boot tests, running fuzzing, etc.
// It's assumed that the workflow will query the necessary data and report its detailed progress itself,
// so we only need to be able to start it and to check its current overall state.
type Service interface {
	Start(sessionID string) error
	Status(id string) (Status, []byte, error)
	// The recommended value. May depend on the implementation (test/prod).
	PollPeriod() time.Duration
}

type Status string

const (
	StatusNotFound Status = "not_found"
	StatusRunning  Status = "running"
	StatusFinished Status = "finished"
	StatusFailed   Status = "failed"
)

// MockService serializes callback invocations to simplify test implementations.
type MockService struct {
	mu             sync.Mutex
	PollDelayValue time.Duration
	OnStart        func(string) error
	OnStatus       func(string) (Status, []byte, error)
}

func (ms *MockService) Start(id string) error {
	ms.mu.Lock()
	defer ms.mu.Unlock()
	if ms.OnStart != nil {
		return ms.OnStart(id)
	}
	return nil
}

func (ms *MockService) Status(id string) (Status, []byte, error) {
	ms.mu.Lock()
	defer ms.mu.Unlock()
	if ms.OnStatus != nil {
		return ms.OnStatus(id)
	}
	return StatusNotFound, nil, nil
}

func (ms *MockService) PollPeriod() time.Duration {
	return ms.PollDelayValue
}