run.go
1.8 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
43
44
45
46
47
48
49
50
51
52
53
package task
import (
"context"
"encoding/json"
"fmt"
"github.com/hibiken/asynq"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/service"
"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: ":6379"},
asynq.Config{Concurrency: 1},
)
h := asynq.NewServeMux()
// ... Register handlers
h.HandleFunc(domain.TaskKeyPatternProductRecordStatics(), HandlerProductRecordStatics)
h.HandleFunc(domain.TaskKeyWorkshopWorkTimeRecordStatics(), WorkshopWorkTimeRecordStatics)
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()
}
// 生产记录统计
func HandlerProductRecordStatics(c context.Context, t *asynq.Task) error {
productPlanService := service.NewProductRecordService(nil)
cmd := &command.ProductRecordStaticsCommand{}
if err := json.Unmarshal(t.Payload(), cmd); err != nil {
return err
}
log.Logger.Debug(fmt.Sprintf("【生产记录统计】 消费 生产记录ID:%v 类型:%v 工段:%v(%v) 重量:%v", cmd.ProductRecordId, cmd.ProductRecordType,
cmd.WorkStation.SectionName, cmd.WorkStation.WorkStationId, cmd.ProductRecordInfo.Weigh))
_, err := productPlanService.ProductRecordStatics(cmd)
if err != nil {
log.Logger.Error(err.Error())
}
return err
}