Debian Codegraph @

api/v0/examples/executor/main.go

raw symbols SCIP
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package main
20
21 import (
22 "flag"
23 "fmt"
24 "math/rand"
25 "time"
26
27 exec "github.com/mesos/mesos-go/api/v0/executor"
28 mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
29 )
30
31 var (
32 slowTasks = flag.Bool("slow_tasks", false, "When true tasks will take several seconds before responding with TASK_FINISHED; useful for debugging failover")
33 )
34
35 type exampleExecutor struct {
36 tasksLaunched int
37 }
38
39 func newExampleExecutor() *exampleExecutor {
40 return &exampleExecutor{tasksLaunched: 0}
41 }
42
43 func (exec *exampleExecutor) Registered(driver exec.ExecutorDriver, execInfo *mesos.ExecutorInfo, fwinfo *mesos.FrameworkInfo, slaveInfo *mesos.SlaveInfo) {
44 fmt.Println("Registered Executor on slave ", slaveInfo.GetHostname())
45 }
46
47 func (exec *exampleExecutor) Reregistered(driver exec.ExecutorDriver, slaveInfo *mesos.SlaveInfo) {
48 fmt.Println("Re-registered Executor on slave ", slaveInfo.GetHostname())
49 }
50
51 func (exec *exampleExecutor) Disconnected(driver exec.ExecutorDriver) {
52 fmt.Println("Executor disconnected.")
53 }
54
55 func (exec *exampleExecutor) LaunchTask(driver exec.ExecutorDriver, taskInfo *mesos.TaskInfo) {
56 fmt.Println("Launching task", taskInfo.GetName(), "with command", taskInfo.Command.GetValue())
57
58 runStatus := &mesos.TaskStatus{
59 TaskId: taskInfo.GetTaskId(),
60 State: mesos.TaskState_TASK_RUNNING.Enum(),
61 }
62 _, err := driver.SendStatusUpdate(runStatus)
63 if err != nil {
64 fmt.Println("Got error", err)
65 }
66
67 exec.tasksLaunched++
68 fmt.Println("Total tasks launched ", exec.tasksLaunched)
69 //
70 // this is where one would perform the requested task
71 //
72 finishTask := func() {
73 // finish task
74 fmt.Println("Finishing task", taskInfo.GetName())
75 finStatus := &mesos.TaskStatus{
76 TaskId: taskInfo.GetTaskId(),
77 State: mesos.TaskState_TASK_FINISHED.Enum(),
78 }
79 if _, err := driver.SendStatusUpdate(finStatus); err != nil {
80 fmt.Println("error sending FINISHED", err)
81 }
82 fmt.Println("Task finished", taskInfo.GetName())
83 }
84 if *slowTasks {
85 starting := &mesos.TaskStatus{
86 TaskId: taskInfo.GetTaskId(),
87 State: mesos.TaskState_TASK_STARTING.Enum(),
88 }
89 if _, err := driver.SendStatusUpdate(starting); err != nil {
90 fmt.Println("error sending STARTING", err)
91 }
92 delay := time.Duration(rand.Intn(90)+10) * time.Second
93 go func() {
94 time.Sleep(delay) // TODO(jdef) add jitter
95 finishTask()
96 }()
97 } else {
98 finishTask()
99 }
100 }
101
102 func (exec *exampleExecutor) KillTask(driver exec.ExecutorDriver, taskID *mesos.TaskID) {
103 fmt.Println("Kill task")
104 }
105
106 func (exec *exampleExecutor) FrameworkMessage(driver exec.ExecutorDriver, msg string) {
107 fmt.Println("Got framework message: ", msg)
108 }
109
110 func (exec *exampleExecutor) Shutdown(driver exec.ExecutorDriver) {
111 fmt.Println("Shutting down the executor")
112 }
113
114 func (exec *exampleExecutor) Error(driver exec.ExecutorDriver, err string) {
115 fmt.Println("Got error message:", err)
116 }
117
118 // -------------------------- func inits () ----------------- //
119 func init() {
120 flag.Parse()
121 }
122
123 func main() {
124 fmt.Println("Starting Example Executor (Go)")
125
126 dconfig := exec.DriverConfig{
127 Executor: newExampleExecutor(),
128 }
129 driver, err := exec.NewMesosExecutorDriver(dconfig)
130
131 if err != nil {
132 fmt.Println("Unable to create a ExecutorDriver ", err.Error())
133 }
134
135 _, err = driver.Start()
136 if err != nil {
137 fmt.Println("Got error:", err)
138 return
139 }
140 fmt.Println("Executor process has started and running.")
141 _, err = driver.Join()
142 if err != nil {
143 fmt.Println("driver failed:", err)
144 }
145 fmt.Println("executor terminating")
146 }