|
| 1 | +/* |
| 2 | + * === This file is part of ALICE O² === |
| 3 | + * |
| 4 | + * Copyright 2018-2022 CERN and copyright holders of ALICE O². |
| 5 | + * Author: Teo Mrnjavac <teo.mrnjavac@cern.ch> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * In applying this license CERN does not waive the privileges and |
| 21 | + * immunities granted to it by virtue of its status as an |
| 22 | + * Intergovernmental Organization or submit itself to any jurisdiction. |
| 23 | + */ |
| 24 | + |
| 25 | +package executor |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "encoding/json" |
| 30 | + |
| 31 | + "github.com/AliceO2Group/Control/common/event" |
| 32 | + "github.com/AliceO2Group/Control/common/utils" |
| 33 | + "github.com/AliceO2Group/Control/common/utils/uid" |
| 34 | + "github.com/AliceO2Group/Control/executor/executable" |
| 35 | + mesos "github.com/mesos/mesos-go/api/v1/lib" |
| 36 | + "github.com/mesos/mesos-go/api/v1/lib/executor/calls" |
| 37 | + "github.com/sirupsen/logrus" |
| 38 | +) |
| 39 | + |
| 40 | +func makeSendStatusUpdateFunc(state *internalState, task mesos.TaskInfo) executable.SendStatusFunc { |
| 41 | + return func(envId uid.ID, mesosState mesos.TaskState, message string) { |
| 42 | + status := newStatus(envId, state, task.TaskID) |
| 43 | + status.State = &mesosState |
| 44 | + status.Message = utils.ProtoString(message) |
| 45 | + state.statusCh <- status |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func makeSendDeviceEventFunc(state *internalState) executable.SendDeviceEventFunc { |
| 50 | + return func(envId uid.ID, event event.DeviceEvent) { |
| 51 | + jsonEvent, err := json.Marshal(event) |
| 52 | + if err != nil { |
| 53 | + log.WithError(err). |
| 54 | + Warning("error marshaling event from task") |
| 55 | + return |
| 56 | + } |
| 57 | + state.messageCh <- jsonEvent |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func makeSendMessageFunc(state *internalState) executable.SendMessageFunc { |
| 62 | + return func(message []byte) { |
| 63 | + // to send task events using state. |
| 64 | + state.messageCh <- message |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func sendOutgoingMessage(state *internalState, message []byte) { |
| 69 | + _, _ = state.cli.Send(context.TODO(), calls.NonStreaming(calls.Message(message))) |
| 70 | +} |
| 71 | + |
| 72 | +func performStatusUpdate(state *internalState, status mesos.TaskStatus) { |
| 73 | + if status.State == nil { |
| 74 | + log.Warn("status with nil state received") |
| 75 | + } else if *status.State == mesos.TASK_FAILED { // failed task updates are sent separately with less priority |
| 76 | + state.activeTasksMu.Lock() |
| 77 | + state.failedTasks[status.TaskID] = status |
| 78 | + delete(state.activeTasks, status.TaskID) |
| 79 | + state.activeTasksMu.Unlock() |
| 80 | + } else { |
| 81 | + switch *status.State { |
| 82 | + case mesos.TASK_DROPPED: |
| 83 | + fallthrough |
| 84 | + case mesos.TASK_FINISHED: |
| 85 | + fallthrough |
| 86 | + case mesos.TASK_GONE: |
| 87 | + fallthrough |
| 88 | + case mesos.TASK_KILLED: |
| 89 | + fallthrough |
| 90 | + case mesos.TASK_LOST: |
| 91 | + state.activeTasksMu.Lock() |
| 92 | + delete(state.activeTasks, status.TaskID) |
| 93 | + state.activeTasksMu.Unlock() |
| 94 | + } |
| 95 | + err := update(state, status) |
| 96 | + if err != nil { // in case of failed update, we just print an error message |
| 97 | + log.WithFields(logrus.Fields{ |
| 98 | + "task": status.TaskID, |
| 99 | + "state": status.State.String(), |
| 100 | + }). |
| 101 | + Warn("executor failed to send task status update") |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments