-
Notifications
You must be signed in to change notification settings - Fork 23
/
scheduling.go
110 lines (99 loc) · 3.87 KB
/
scheduling.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scheduling
import (
"encoding/json"
"path"
"strings"
"time"
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"github.com/ystia/yorc/v3/helper/consulutil"
"github.com/ystia/yorc/v3/log"
"github.com/ystia/yorc/v3/prov"
)
// RegisterAction allows to register a scheduled action and to start scheduling it
func RegisterAction(client *api.Client, deploymentID string, timeInterval time.Duration, action *prov.Action) (string, error) {
log.Debugf("Action with ID:%q has been requested to be registered for scheduling with [deploymentID:%q, timeInterval:%q]", action.ID, deploymentID, timeInterval.String())
id := uuid.NewV4().String()
// Check mandatory parameters
if deploymentID == "" {
return "", errors.New("deploymentID is mandatory parameter to register scheduled action")
}
if action == nil || action.ActionType == "" {
return "", errors.New("actionType is mandatory parameter to register scheduled action")
}
asyncOp, err := json.Marshal(action.AsyncOperation)
if err != nil {
return "", errors.Wrapf(err, "Failed to generate async operation representation for action %q", action.ID)
}
scaPath := path.Join(consulutil.SchedulingKVPrefix, "actions", id)
scaOps := api.KVTxnOps{
&api.KVTxnOp{
Verb: api.KVSet,
Key: path.Join(scaPath, "deploymentID"),
Value: []byte(deploymentID),
},
&api.KVTxnOp{
Verb: api.KVSet,
Key: path.Join(scaPath, "type"),
Value: []byte(action.ActionType),
},
&api.KVTxnOp{
Verb: api.KVSet,
Key: path.Join(scaPath, "interval"),
Value: []byte(timeInterval.String()),
},
&api.KVTxnOp{
Verb: api.KVSet,
Key: path.Join(scaPath, "async_op"),
Value: asyncOp,
},
}
if action.Data != nil {
for k, v := range action.Data {
scaOps = append(scaOps, &api.KVTxnOp{
Verb: api.KVSet,
Key: path.Join(scaPath, "data", k),
Value: []byte(v),
})
}
}
ok, response, _, err := client.KV().Txn(scaOps, nil)
if err != nil {
return "", errors.Wrapf(err, "Failed to register scheduled action for deploymentID:%q, type:%q, id:%q", deploymentID, action.ActionType, id)
}
if !ok {
// Check the response
errs := make([]string, 0)
for _, e := range response.Errors {
errs = append(errs, e.What)
}
return "", errors.Errorf("Failed to register scheduled action for deploymentID:%q, type:%q, id:%q due to:%s", deploymentID, action.ActionType, id, strings.Join(errs, ", "))
}
return id, nil
}
// UnregisterAction allows to unregister a scheduled action and to stop scheduling it
func UnregisterAction(client *api.Client, id string) error {
log.Debugf("Unregister scheduled action with id:%q", id)
scaPath := path.Join(consulutil.SchedulingKVPrefix, "actions", id)
return errors.Wrap(consulutil.StoreConsulKeyAsString(path.Join(scaPath, ".unregisterFlag"), "true"), "Failed to flag scheduled action for removal")
}
// UpdateActionData updates the value of a given data within an action
func UpdateActionData(client *api.Client, id, key, value string) error {
//TODO check if action exists
scaKeyPath := path.Join(consulutil.SchedulingKVPrefix, "actions", id, "data", key)
return errors.Wrapf(consulutil.StoreConsulKeyAsString(scaKeyPath, value), "Failed to update data %q for action %q", key, id)
}