/
que.go
59 lines (51 loc) · 1.54 KB
/
que.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
package cque
import (
"log"
)
// Job is a single unit of work for Que to perform.
type Job struct {
Type string
// Args can be anything depends on the job
Args interface{}
}
// Result is the output of a Job and can be used to send something back to mainthread.
type Result struct {
JobType string
// Args can be anything depends on the job
Result interface{}
}
// Client is a Que client that can add jobs to the queue and remove jobs from
// the queue.
type Client struct {
IsQueueEmpty bool
pool chan Job
workerstatus chan WorkerWaitStatus
Result chan Result
// TODO: add a way to specify default queueing options
}
type WorkerWaitStatus struct {
Id int
WaitStatus bool
}
// NewClient create our new local queue
// Note: we are creating 5000 queue as we expect this to not reach this high.
// If due to delay and this limit is reached, it may cause deadlock if all channels want to write
// This was implemented crudely as PoC so fixes should be considered if used in production.
// Eg: maybe a slice/array to store the jobs being added to the client and have a channel dedicated to pushing
// the job out to workers.
func NewQue() *Client {
return &Client{
IsQueueEmpty: true,
pool: make(chan Job, 5000),
workerstatus: make(chan WorkerWaitStatus, 100),
Result: make(chan Result, 5000),
}
}
// Enqueue adds a job to the queue.
func (c *Client) Enqueue(j Job) {
c.pool <- j
}
// We dont tolerate error, all error will be logged and discarded.
func (j *Job) Error(msg string) {
log.Printf("[ERROR] %s\n", msg)
}