Example #1
0
        /// <summary>
        ///  THIS ASSUMES THAT IT IS CALLED ON A THREAD HOLDING THE LOCK ON THE HeartBeatManager
        /// </summary>
        /// <param name="e"></param>
        private void HandleTaskInitializationException(TaskClientCodeException e)
        {
            byte[] error;
            try
            {
                error = ByteUtilities.SerializeToBinaryFormat(e);
            }
            catch (SerializationException se)
            {
                error = ByteUtilities.SerializeToBinaryFormat(
                    TaskClientCodeException.CreateWithNonSerializableInnerException(e, se));
            }

            var avroFailedTask = new AvroFailedTask
            {
                identifier = e.TaskId,
                cause = error,
                data = ByteUtilities.StringToByteArrays(e.ToString()),
                message = e.Message
            };

            var taskStatus = new TaskStatusProto
            {
                context_id = e.ContextId,
                task_id = e.TaskId,
                result = AvroJsonSerializer<AvroFailedTask>.ToBytes(avroFailedTask),
                state = State.FAILED
            };

            LOGGER.Log(Level.Error, "Sending Heartbeat for a failed task: {0}", taskStatus);
            _heartBeatManager.OnNext(taskStatus);
        }
Example #2
0
        public TaskStatusProto ToProto()
        {
            // This is locked because the Task continuation thread which sets the
            // result is potentially different from the HeartBeat thread.
            lock (_heartBeatManager)
            {
                Check();
                TaskStatusProto taskStatusProto = new TaskStatusProto()
                {
                    context_id = ContextId,
                    task_id = TaskId,
                    state = GetProtoState()
                };
                if (_result.IsPresent())
                {
                    taskStatusProto.result = ByteUtilities.CopyBytesFrom(_result.Value);
                }
                else if (_lastException.IsPresent())
                {
                    byte[] error;
                    try
                    {
                        error = ByteUtilities.SerializeToBinaryFormat(_lastException.Value);
                    }
                    catch (SerializationException se)
                    {
                        error = ByteUtilities.SerializeToBinaryFormat(
                            NonSerializableTaskException.UnableToSerialize(_lastException.Value, se));
                    }

                    var avroFailedTask = new AvroFailedTask
                    {
                        identifier = _taskId,
                        cause = error,
                        data = ByteUtilities.StringToByteArrays(_lastException.Value.ToString()),
                        message = _lastException.Value.Message
                    };

                    taskStatusProto.result = AvroJsonSerializer<AvroFailedTask>.ToBytes(avroFailedTask);
                }
                else if (_state == TaskState.Running)
                {
                    foreach (TaskMessage message in GetMessages())
                    {
                        TaskStatusProto.TaskMessageProto taskMessageProto = new TaskStatusProto.TaskMessageProto()
                        {
                            source_id = message.MessageSourceId,
                            message = ByteUtilities.CopyBytesFrom(message.Message),
                        };
                        taskStatusProto.task_message.Add(taskMessageProto);
                    }
                }
                return taskStatusProto;
            }
        }