zqueu.go 766 Bytes
Newer Older
Vladimir Barsukov's avatar
Vladimir Barsukov committed
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
package zqueue

import (
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com:barsukov-vv/komrad/zgin"
	"sync"
)

type ZQueue struct {
	queue map[string]chan *ZJob

	mu sync.Mutex
}

type ZJob struct {
	Id     int64    `json:"id"`
	Body   any      `json:"body"`
	Result chan any `json:"-"`

	Wg *sync.WaitGroup `json:"-"`
}

func New() *ZQueue {
	return &ZQueue{}
}
func Default() *ZQueue {
	return &ZQueue{}
}

func (q *ZQueue) GetHandler(c *gin.Context) {
	queue := c.Param("queue")

	if qChan, ok := q.queue[queue]; !ok {
		zgin.Err(c, errors.New(fmt.Sprintf("unknown queue: '%s'", queue)))
	} else {
		job := <-qChan

		zgin.Ok(c, job.Body)

		return
	}
}

func (q *ZQueue) PostHandler(c *gin.Context) {

}

func (q *ZQueue) NewHandler(c *gin.Context) {

}