asynq_service.go 1.4 KB
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
}