-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathrtustoplogic.go
90 lines (83 loc) · 1.88 KB
/
rtustoplogic.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package logic
import (
"context"
"encoding/json"
"errors"
"strconv"
"github.com/zeromicro/cds/cmd/galaxy/internal/clients"
"github.com/zeromicro/cds/cmd/galaxy/internal/svc"
"github.com/zeromicro/cds/cmd/galaxy/internal/types"
"github.com/zeromicro/cds/cmd/rtu/cmd/sync/config"
"github.com/zeromicro/cds/pkg/strx"
"github.com/zeromicro/go-zero/core/logx"
)
type RtuStopLogic struct {
ctx context.Context
logx.Logger
svcCtx *svc.ServiceContext
}
func NewRtuStopLogic(ctx context.Context, svcCtx *svc.ServiceContext) RtuStopLogic {
return RtuStopLogic{
ctx: ctx,
Logger: logx.WithContext(ctx),
svcCtx: svcCtx,
}
// TODO need set model here from svc
}
func (l *RtuStopLogic) RtuStop(req types.String) error {
cli := clients.NewRtuClient(l.svcCtx.EtcdClient)
status, e := cli.Status(req.String)
if e != nil {
logx.Error(e)
return e
}
id, err := strconv.Atoi(req.String)
if err != nil {
logx.Error(e)
return e
}
e = l.svcCtx.RtuModel.Update(id, "stop")
if e != nil {
logx.Error(e)
return e
}
exists, err := l.svcCtx.RtuModel.GetExist()
if err != nil {
logx.Error(err)
return err
}
jobs := make([]config.Job, 0, len(exists))
for _, i := range exists {
s, err := strx.DecryptDsn(i.TargetShards)
if err != nil {
logx.Error(err)
continue
}
shards := new([]string)
err = json.Unmarshal([]byte(s), shards)
if err != nil {
logx.Error(err)
continue
}
dsn, err := strx.DecryptDsn(i.SourceDsn)
if err != nil {
logx.Error(err)
continue
}
job, err := buildJob(dsn, strconv.Itoa(i.ID), i.SourceType, i.TargetTable, i.SourceQueryKey, i.TargetDB, *shards)
if err != nil {
logx.Error(err)
continue
}
jobs = append(jobs, *job)
}
err = cli.StartJobs(jobs)
if err != nil {
logx.Error(err)
return err
}
if status.Status != config.STATUS_RUNNING {
return errors.New("状态为" + status.Status + ",无需停止")
}
return nil
}