-
Notifications
You must be signed in to change notification settings - Fork 167
/
fake.go
121 lines (106 loc) · 3.29 KB
/
fake.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
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package scheduler
import (
"context"
"fmt"
"strconv"
"time"
"github.com/micro/go-micro/client"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/scheduler/actions"
)
var (
fakeActionName = "actions.test.fake"
)
type FakeAction struct {
timer int64
ticker int64
}
// GetName returns this action unique identifier
func (f *FakeAction) GetName() string {
return fakeActionName
}
// Implement ControllableAction
func (f *FakeAction) CanPause() bool {
return true
}
// Implement ControllableAction
func (f *FakeAction) CanStop() bool {
return true
}
// ProvidesProgress mocks ProgressProviderAction interface method
func (f *FakeAction) ProvidesProgress() bool {
return true
}
// Init passes parameters to the action
func (f *FakeAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
f.timer = 10
f.ticker = 3
if strTime, ok := action.Parameters["timer"]; ok {
if timer, err := strconv.ParseInt(strTime, 10, 64); err == nil {
f.timer = timer
}
}
if strTime, ok := action.Parameters["ticker"]; ok {
if ticker, err := strconv.ParseInt(strTime, 10, 64); err == nil {
f.ticker = ticker
}
}
return nil
}
// Run performs the actual action code
func (f *FakeAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
outputMessage := input
if len(input.Nodes) > 0 {
log.TasksLogger(ctx).Info("Fake task received node "+input.Nodes[0].Path, input.Nodes[0].Zap())
return outputMessage, nil
}
log.TasksLogger(ctx).Info("Starting fake long task")
outputMessage.AppendOutput(&jobs.ActionOutput{StringBody: "Hello World"})
ticker := time.NewTicker(time.Second * time.Duration(f.ticker))
go func() {
time.Sleep(time.Second * time.Duration(f.timer))
ticker.Stop()
}()
steps := float32(f.timer) / float32(f.ticker)
step := float32(0)
loop:
for {
select {
case t := <-ticker.C:
channels.Progress <- step * 100 / steps
step++
message := fmt.Sprintf("Ticking Now %v", t)
log.TasksLogger(ctx).Info(message)
channels.StatusMsg <- message
case <-channels.Pause:
log.TasksLogger(ctx).Info("fake task received pause from channels, should pause here")
<-channels.BlockUntilResume()
log.TasksLogger(ctx).Info("blockuntilresume passed, received resume, continue")
case <-channels.Stop:
log.TasksLogger(ctx).Info("received stop from channels: interrupting")
ticker.Stop()
break loop
}
}
return outputMessage, nil
}