asynq_run.go 1.2 KB
package task

import (
	"github.com/hibiken/asynq"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
	"os"
	"os/signal"
	"syscall"
)

func Run() {
	srv := asynq.NewServer(
		asynq.RedisClientOpt{Addr: constant.REDIS_ADDRESS},
		asynq.Config{Concurrency: 1},
	)

	h := asynq.NewServeMux()
	// ... Register handlers
	h.HandleFunc(domain.TaskKeyPatternProductRecordStatics(), HandlerProductRecordStatics)
	h.HandleFunc(domain.TaskKeyWorkshopWorkTimeRecordStatics(), WorkshopWorkTimeRecordStatics)
	h.HandleFunc(domain.TaskDeviceZkTecoReport(), WorkerAttendanceReport)
	h.HandleFunc(domain.TaskDeviceCollection(), WorkshopDataConsumer)
	log.Logger.Info("aysnq task running ...")
	// Run blocks and waits for os signal to terminate the program.
	if err := srv.Run(h); err != nil {
		log.Logger.Error(err.Error())
	}
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
	<-sigs // wait for termination signal
	log.Logger.Info("aysnq task stopping ...")
	srv.Shutdown()
}