crontab_sync_product.go
1.6 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
54
55
56
57
58
59
package crontab
import (
"context"
"fmt"
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
// SyncProduct 定时同步产品
func SyncProduct(ctx context.Context) error {
defer func() {
if r := recover(); r != nil {
log.Logger.Error(fmt.Sprintf("%v", r))
}
}()
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
if err != nil {
log.Logger.Error("【定时同步产品信息】 失败:" + err.Error())
}
transactionContext.RollbackTransaction()
}()
log.Logger.Debug("【定时同步同步产品信息】 启动")
pullK3CloudService := syncdata.PullDataK3CloudService{}
t := ctx.Value("fromTime")
var fromTime time.Time
if t != nil {
if v, ok := t.(time.Time); ok {
fromTime = v
}
}
if fromTime.IsZero() {
fromTime, err = redis.GetLastFetchTime(redis.MaterialK3cloudFetchTimeKey())
if err != nil {
log.Logger.Error(err.Error())
return nil
}
}
if err := pullK3CloudService.SyncDataProduct(transactionContext.(*pg.TransactionContext), fromTime); err != nil {
log.Logger.Error(err.Error())
return nil
}
if err = transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}