-
Notifications
You must be signed in to change notification settings - Fork 168
/
runnable.go
153 lines (134 loc) · 4.51 KB
/
runnable.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
/*
* 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 tasks
import (
"context"
"fmt"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/golang/protobuf/proto"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/scheduler/actions"
)
// Runnable represents the runnable instance of a given task
type Runnable struct {
jobs.Action
Task *Task
Message jobs.ActionMessage
Client client.Client
Context context.Context
Implementation actions.ConcreteAction
}
func RootRunnable(ctx context.Context, cl client.Client, task *Task) Runnable {
return Runnable{
Context: ctx,
Client: cl,
Task: task,
}
}
// NewRunnable creates a new runnable and populates it with the concrete task implementation found with action.ID,
// if such an implementation is found.
func NewRunnable(ctx context.Context, cl client.Client, task *Task, action *jobs.Action, message jobs.ActionMessage) Runnable {
r := Runnable{
Action: *action,
Task: task,
Client: cl,
Context: ctx,
Message: message,
}
// Find Concrete Implementation from ActionID
impl, ok := actions.GetActionsManager().ActionById(action.ID)
if ok {
r.Implementation = impl
r.Implementation.Init(task.Job, cl, action)
}
return r
}
// CreateChild replicates a runnable for child action
func (r *Runnable) CreateChild(action *jobs.Action, message jobs.ActionMessage) Runnable {
r.Task.Add(1)
return NewRunnable(r.Context, r.Client, r.Task, action, message)
}
// Dispatch gets next runnable from Action and enqueues it to the Queue
// Todo - Check that done channel is working correctly with chained actions
func (r *Runnable) Dispatch(input jobs.ActionMessage, actions []*jobs.Action, Queue chan Runnable) {
for _, action := range actions {
act := action
messagesOutput := make(chan jobs.ActionMessage)
done := make(chan bool, 1)
go func() {
defer func() {
close(messagesOutput)
close(done)
}()
for {
select {
case message := <-messagesOutput:
// Build runnable and enqueue
m := proto.Clone(&message).(*jobs.ActionMessage)
Queue <- r.CreateChild(act, *m)
case <-done:
return
}
}
}()
action.ToMessages(input, r.Client, r.Context, messagesOutput, done)
}
}
// RunAction creates an action and calls Dispatch
func (r *Runnable) RunAction(Queue chan Runnable) error {
if r.Implementation == nil {
return errors.NotFound(common.SERVICE_JOBS, fmt.Sprintf("cannot run action: no concrete implementation found for ID %s, are you sure this action has been correctly registered?", r.Action.ID))
}
taskUpdateDelegated := false
if taskConsumer, ok := (r.Implementation).(actions.TaskUpdaterDelegateAction); ok {
taskUpdateDelegated = true
taskConsumer.SetTask(r.Task.GetJobTaskClone())
}
if controllable, ok := r.Implementation.(actions.ControllableAction); ok {
r.Task.SetControllable(controllable.CanStop(), controllable.CanPause())
}
if progressProvider, ok := r.Implementation.(actions.ProgressProviderAction); ok && progressProvider.ProvidesProgress() {
r.Task.SetHasProgress()
}
r.Task.SetStatus(jobs.TaskStatus_Running)
r.Task.SetStartTime(time.Now())
r.Task.Save()
runnableChannels, done := r.Task.GetRunnableChannels()
outputMessage, err := r.Implementation.Run(r.Context, runnableChannels, r.Message)
done <- true
r.Task.Done(1)
if err != nil {
r.Task.SetStatus(jobs.TaskStatus_Error, "Error: "+err.Error())
r.Task.SetEndTime(time.Now())
r.Task.Save()
return err
}
r.Task.AppendLog(r.Action, r.Message, outputMessage)
r.Dispatch(outputMessage, r.ChainedActions, Queue)
if !taskUpdateDelegated {
r.Task.SetStatus(jobs.TaskStatus_Finished, "Complete")
r.Task.SetEndTime(time.Now())
r.Task.Save()
}
return nil
}