-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
59 lines (47 loc) · 1.28 KB
/
workers.go
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 (C) 2023 Martijn van der Kleijn
This file is part of the go-simplequeue library.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package simplequeue
import (
"context"
"fmt"
"runtime/trace"
)
// WorkerI is intended for possible future expansion
type WorkerI interface {
ID() int64
Handled() int64
Process(job Job)
}
// Worker is the implementation of a single worker
type Worker struct {
id int64
jobsHandled int64
}
// ID returns the worker's ID
func (w *Worker) ID() int64 {
return w.id
}
// Handled returns the number of jobs the worker handled
func (w *Worker) Handled() int64 {
return w.jobsHandled
}
func (w *Worker) process(ctx context.Context, job Job) {
job.Do()
w.jobsHandled++
trace.Log(ctx, "process", fmt.Sprintf("worker %v finished Job %v.", w.ID(), job.ID()))
}
// InitializeWorkers initializes and returns a pool of workers
func InitializeWorkers(ctx context.Context, num int) []*Worker {
_, task := trace.NewTask(ctx, "initialize workers")
workers := make([]*Worker, 0)
for i := 1; i <= num; i++ {
workers = append(workers, &Worker{id: int64(i)})
}
task.End()
return workers
}