50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/rabbitmq"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
const appWorkerBatchExecuteQueueDefault = "iotlcm.batch.execute"
|
|
|
|
type appWorkerBatchMessage struct {
|
|
BatchNo string `json:"batchNo"`
|
|
BatchType int `json:"batchType"`
|
|
}
|
|
|
|
func appWorkerBatchAsyncEnabled(ctx context.Context) bool {
|
|
return g.Cfg().MustGet(ctx, "workerBatchAsync.enabled", false).Bool()
|
|
}
|
|
|
|
func (s *sAppCard) publishWorkerBatchExecute(ctx context.Context, batchNo string, batchType int, requirePublication ...bool) error {
|
|
if strings.TrimSpace(batchNo) == "" {
|
|
return gerror.New("批次号不能为空")
|
|
}
|
|
if !appWorkerBatchAsyncEnabled(ctx) {
|
|
return nil
|
|
}
|
|
queueName := strings.TrimSpace(g.Cfg().MustGet(ctx, "workerBatchAsync.queueName", appWorkerBatchExecuteQueueDefault).String())
|
|
if queueName == "" {
|
|
queueName = appWorkerBatchExecuteQueueDefault
|
|
}
|
|
|
|
err := rabbitmq.Publish(ctx, queueName, appWorkerBatchMessage{
|
|
BatchNo: strings.TrimSpace(batchNo),
|
|
BatchType: batchType,
|
|
})
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "投递 worker 批次失败, queue:%s, batchNo:%s, batchType:%d, err:%+v", queueName, batchNo, batchType, err)
|
|
if (len(requirePublication) > 0 && requirePublication[0]) || g.Cfg().MustGet(ctx, "workerBatchAsync.failOnPublish", true).Bool() {
|
|
return gerror.Wrap(err, "批次已创建但投递 worker 失败")
|
|
}
|
|
return nil
|
|
}
|
|
g.Log().Infof(ctx, "投递 worker 批次成功, queue:%s, batchNo:%s, batchType:%d", queueName, batchNo, batchType)
|
|
return nil
|
|
}
|