aboutsummaryrefslogtreecommitdiff
path: root/bus.go
blob: d4911138b0ece6b46e0c12c93c126ad4c2826a0f (plain) (blame)
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
package fqueue

import (
	"errors"
	"os"
	"path/filepath"
	"time"
)

const (
	seqFile       = "_seq.txt"
	queueDir      = "queue"
	processingDir = "processing"

	// Максимальное время сколько задача может находиться в статусе
	// «обрабатывается» самой шиной. При этом не учитывается время на обработку
	// задачи пользовательским кодом, а только с момента как *шина*
	// заблокировала задачу и до момента её передачи из NextTask.
	// Умозрительно считаю, что секунда это даже много.
	maxProcessTime = time.Second
)

var (
	ErrCantGetExLock = errors.New("cant get exclusive lock")
)

type Bus struct {
	queuePath      string
	processingPath string
	seqFile        string
}

func New(path string) (*Bus, error) {
	queuePath := filepath.Join(path, queueDir)
	processingPath := filepath.Join(path, processingDir)
	if err := os.MkdirAll(queuePath, 0o700); err != nil {
		return nil, err
	}
	if err := os.MkdirAll(processingPath, 0o700); err != nil {
		return nil, err
	}

	return &Bus{
		queuePath:      queuePath,
		processingPath: processingPath,
		seqFile:        filepath.Join(path, seqFile),
	}, nil
}