forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
174 lines (153 loc) · 4.5 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* 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"
"time"
"github.com/micro/go-micro/client"
"github.com/pydio/cells/common/forms"
"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 string
ticker string
}
func (f *FakeAction) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: fakeActionName,
Label: "Sleep",
Icon: "clock-end",
Category: actions.ActionCategoryScheduler,
Description: "Use as a waiter, or simulate a long-running process with progress",
SummaryTemplate: "",
HasForm: true,
}
}
func (f *FakeAction) GetParametersForm() *forms.Form {
return &forms.Form{Groups: []*forms.Group{
{
Fields: []forms.Field{
&forms.FormField{
Name: "timer",
Type: forms.ParamInteger,
Label: "Total time",
Description: "Total task time in seconds",
Default: 10,
Mandatory: true,
Editable: true,
},
&forms.FormField{
Name: "ticker",
Type: forms.ParamInteger,
Label: "Ticks",
Description: "Time between each ticks for incrementing progress, in seconds",
Default: 3,
Mandatory: false,
Editable: true,
},
},
},
}}
}
// 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 = ""
if strTime, ok := action.Parameters["timer"]; ok {
f.timer = strTime
}
if strTime, ok := action.Parameters["ticker"]; ok {
f.ticker = strTime
}
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
var timer, tick int64
if t, err := jobs.EvaluateFieldInt64(ctx, input, f.timer); err == nil {
timer = t
} else {
return input.WithError(err), err
}
if f.ticker != "" {
if t, err := jobs.EvaluateFieldInt64(ctx, input, f.ticker); err == nil {
tick = t
}
} else {
tick = timer
}
log.TasksLogger(ctx).Info("Starting fake long task")
outputMessage.AppendOutput(&jobs.ActionOutput{StringBody: "Hello World"})
ticker := time.NewTicker(time.Second * time.Duration(tick))
finished := make(chan struct{}, 1)
go func() {
<-time.After(time.Second * time.Duration(timer))
close(finished)
}()
steps := float32(timer) / float32(tick)
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("block-until-resume passed, received resume, continue")
case <-channels.Stop:
log.TasksLogger(ctx).Info("received stop from channels: interrupting")
ticker.Stop()
break loop
case <-finished:
log.TasksLogger(ctx).Info("ticking is now finished")
ticker.Stop()
break loop
}
}
return outputMessage, nil
}