asynq_service.go
1.4 KB
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
package domainService
import (
"fmt"
"github.com/hibiken/asynq"
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
const (
QueueProduct = "product"
QueueDevice = "device"
QueueDefault = "default"
)
func FormatQueue(qt string) string {
return fmt.Sprintf("%v:queue:%v", constant.CACHE_PREFIX, qt)
}
func SendWorkshopWorkTimeStaticJob(r *domain.ProductAttendanceRecord) error {
return SendAsyncJob(domain.TaskKeyWorkshopWorkTimeRecordStatics(), r, asynq.Queue(FormatQueue(QueueDefault)))
}
func SendProductRecordStaticsJob(r *domain.ProductRecord) error {
return SendAsyncJob(domain.TaskKeyPatternProductRecordStatics(), r, asynq.Queue(FormatQueue(QueueProduct)))
}
func SendDeviceZkTecoReportJob(r *domain.DeviceZkTeco) error {
return SendAsyncJob(domain.TaskDeviceZkTecoReport(), r, asynq.Queue(FormatQueue(QueueDefault)))
}
func SendWorkshopDeviceData(r *domain.DeviceCollection) error {
return SendAsyncJob(domain.TaskDeviceCollection(), r, asynq.Queue(FormatQueue(QueueDevice)))
}
func SendAsyncJob(queueName string, job interface{}, opts ...asynq.Option) error {
task := asynq.NewTask(queueName, []byte(json.MarshalToString(job)))
client := asynq.NewClient(asynq.RedisClientOpt{Addr: constant.REDIS_ADDRESS})
_, err := client.Enqueue(task, opts...)
return err
}