-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjobManager.go
117 lines (107 loc) · 3.91 KB
/
jobManager.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
package maestro
// Copyright (c) 2018, Arm Limited and affiliates.
// SPDX-License-Identifier: Apache-2.0
//
// 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.
import (
"net/http"
"sync"
"github.com/PelionIoT/maestro/debugging"
"github.com/PelionIoT/maestro/log"
"github.com/PelionIoT/maestro/processes"
"github.com/PelionIoT/maestro/storage"
"github.com/PelionIoT/maestro/tasks"
"github.com/PelionIoT/maestroSpecs"
)
type jobManagerInstance struct {
// ticker *time.Ticker
// controlChan chan uint32
// aggregateChan chan *imageop
jobops sync.Map // taskId : imageop
}
var JobManagerInstance *jobManagerInstance
func init() {
JobManagerInstance = new(jobManagerInstance)
}
// TODO - jobManager needs to be a TaskHandler... and Jobs should be started like any other Task
//
// Note: JobOperation in maestroSpecs
//
func (this *jobManagerInstance) SubmitTask(task *tasks.MaestroTask) (errout error) {
// if task.Op.GetType() == maestroSpecs.OP_TYPE_JOB {
// requestedOp, ok := task.Op.(*maestroSpecs.JobOpPayload)
// } else {
debugging.DEBUG_OUT("IMAGE>>>JobManagerInstance.SubmitTask() not correct Op type\n")
// err := new(maestroSpecs.APIError)
// err.HttpStatusCode = 500
// err.ErrorString = "op was not an ImageOperation"
// err.Detail = "in SubmitTask()"
// log.MaestroErrorf("JobManagerInstance got missplaced task %+v\n",task)
// errout = err
// }
return nil
}
func (this *jobManagerInstance) ValidateTask(task *tasks.MaestroTask) error {
return nil
}
func ProcessJobOp(DB *storage.MaestroDBInstance, msg maestroSpecs.JobOperation) (joberr *maestroSpecs.APIError, taskid string) {
switch msg.GetOp() {
case maestroSpecs.OP_ADD:
job := msg.GetJobPayload()
if job != nil {
err := DB.UpsertJob(job)
if err != nil {
log.MaestroErrorf("Error on saving job in DB: %s\n", err.Error())
}
err = processes.RegisterJob(job)
if err != nil {
log.MaestroErrorf("Job [%s] failed to register job: %s\n", job.GetJobName(), err.Error())
}
} else {
joberr = &maestroSpecs.APIError{http.StatusNotAcceptable, "Badly formed", "no job data"}
// w.WriteHeader(http.StatusNotAcceptable)
// w.Write([]byte("{\"error\":\"Badly formed\", \"details\":\"no job data\"}"))
}
case maestroSpecs.OP_REMOVE:
params := msg.GetParams()
if len(params) > 0 {
// TODO: removal of a job requires shutdown
// task will need to be queued, and task ID given
} else {
joberr = &maestroSpecs.APIError{http.StatusNotAcceptable, "Badly formed", "no job name(s)"}
// w.WriteHeader(http.StatusNotAcceptable)
// w.Write([]byte("{\"error\":\"Badly formed\", \"details\":\"no job name(s)\"}"))
}
case maestroSpecs.OP_UPDATE:
job := msg.GetJobPayload()
if job != nil {
err := DB.UpsertJob(job)
if err != nil {
log.MaestroErrorf("Error on saving job in DB: %s\n", err.Error())
}
err = processes.RegisterJob(job)
if err != nil {
log.MaestroErrorf("Job [%s] failed to register job: %s\n", job.GetJobName(), err.Error())
}
} else {
joberr = &maestroSpecs.APIError{http.StatusNotAcceptable, "Badly formed", "no job data"}
// w.WriteHeader(http.StatusNotAcceptable)
// w.Write([]byte("{\"error\":\"Badly formed\", \"details\":\"no job data\"}"))
}
default:
joberr = &maestroSpecs.APIError{http.StatusNotAcceptable, "Badly formed", "unknown command"}
// w.WriteHeader(http.StatusNotAcceptable)
// w.Write([]byte("{\"error\":\"Badly formed\", \"details\":\"unknown command\"}"))
}
return
}