public async Task <AcknowledgementMessage> ExecuteTask(JobStartedMessage taskMessage) { return(await Task.Delay(100) .ContinueWith <AcknowledgementMessage>(task => { AcknowledgementReceipt receipt = AcknowledgementReceipt.SUCCESS; long taskTime = 0; Task externalTask = Task.Factory.StartNew(() => { // call or execute an external application here }); // wait for task to complete externalTask.Wait(); switch (externalTask.Status) { case TaskStatus.Faulted: receipt = AcknowledgementReceipt.FAILED; break; case TaskStatus.Canceled: receipt = AcknowledgementReceipt.CANCELED; break; case TaskStatus.RanToCompletion: receipt = AcknowledgementReceipt.SUCCESS; break; } // send the acknowledgement return new AcknowledgementMessage(taskMessage.ID, taskMessage.Description, taskTime, receipt); })); }
/// <summary> /// Perform the exceution of job, Handles "BeginJobMessage" message /// </summary> /// <param name="job"></param> private void HandleJobExecute(JobStartedMessage job) { _logger.Info("Worker has received BeginJobMessage from {0} for Task ID: {1}", Sender.Path.Name, job.ID); _myJob = job; _logger.Info("Worker has started Task ID: {0}", job.ID); // use pipeto to handle async call from caller ( taskexecuter ) _taskExecuter.ExecuteTask(job).PipeTo(Self, Sender); }
/// <summary> /// Perform the exceution of job, Handles "BeginJobMessage" message /// </summary> /// <param name="job"></param> private void HandleJobExecute(JobStartedMessage job) { _myJob = new JobStartedMessage(job.Description, job.ID, job.TimeOut); ColorConsole.WriteLineCyan($"Task {job.ID}. { job.Description} is started with timeout {job.TimeOut.ToString()} minute."); // use pipeto to handle async call from caller ( taskexecuter ) _taskExecuter.ExecuteTask(job).PipeTo(Self, Sender); _taskTimer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable( TimeSpan.FromMinutes(1), //initial delay in minutes TimeSpan.FromSeconds(10), // interval Self, new TaskTimer(), Self); }
private void Asking() { _logger.Debug("Commander's current state is Asking."); // received UnableToAcceptJobMessage from coordinator Receive <UnableToAcceptJobMessage>(job => { _logger.Debug($"Commander has received UnableToAcceptJobMessage from { Sender.Path.Name} for Task ID: {job.ID}"); // each routee is giving response to cordinator _pendingJobReplies--; if (_pendingJobReplies == 0) { // send response to client _currentJob.Client.Tell(job); // move to next state BecomeReady(); } }); // received AbleToAcceptJobMessage from coordinator Receive <AbleToAcceptJobMessage>(job => { ColorConsole.WriteLineCyan($"Starting Job for Task ID: {job.ID}. {job.Description}"); _logger.Debug($"Commander has received AbleToAcceptJobMessage from {Sender.Path.Name} for Task ID: {job.ID}"); var jobMessage = new JobStartedMessage(job.Description, job.ID); Sender.Tell(jobMessage, Self); // tell the client that job has been started _currentJob.Client.Tell(jobMessage); BecomeReady(); }); // means at least one actor failed to respond Receive <ReceiveTimeout>(timeout => { _logger.Warning("Commander has received ReceiveTimeout from {0}", Sender.Path.Name); // move to next state BecomeReady(); }); }
private void Asking() { // received UnableToAcceptJobMessage from coordinator Receive <UnableToAcceptJobMessage>(job => { // each routee is giving response to cordinator _pendingJobReplies--; if (_pendingJobReplies == 0) { // send response to client _currentJob.Client.Tell(job); // move to next state BecomeReady(); } }); // received AbleToAcceptJobMessage from coordinator Receive <AbleToAcceptJobMessage>(job => { ColorConsole.WriteLineYellow($"Starting Job for Task ID: {job.ID}. {job.Description}"); // ask coordinator for processing message var jobMessage = new JobStartedMessage(job.Description, job.ID); Sender.Tell(jobMessage, Self); // tell the client that job has been started _currentJob.Client.Tell(jobMessage); BecomeReady(); }); // means at least one actor failed to respond Receive <ReceiveTimeout>(timeout => { // send response to client ColorConsole.WriteLineYellow($"Receive timeout from { Sender.Path.Name } for Task ID. {_currentJob.ID}. {_currentJob.Description}"); BecomeAsking(); }); }
public async Task <AcknowledgementMessage> ExecuteTask(JobStartedMessage taskMessage) { string outputPath = ConfigurationManager.AppSettings["ClientOutputFolderPath"]; string exePath = ConfigurationManager.AppSettings["ClientExecutablePath"]; string title = string.Format($"Executer : Task {taskMessage.ID}"); outputPath = ConvertToURIPath(outputPath); exePath = ConvertToURIPath(exePath); string extractor1 = outputPath + @"/" + "Extractor1.txt"; string extractor2 = outputPath + @"/" + taskMessage.Description; return(await Task.Delay(100) .ContinueWith <AcknowledgementMessage>(task => { // execute task here long taskTime = 0; int exitCode = 0; AcknowledgementReceipt receipt = AcknowledgementReceipt.SUCCESS; if (string.IsNullOrEmpty(extractor2) || string.IsNullOrEmpty(extractor1) || string.IsNullOrEmpty(outputPath) || string.IsNullOrEmpty(exePath)) { receipt = AcknowledgementReceipt.INVALID_TASK; } else { Task exteranlTask = Task.Factory.StartNew(() => { ExecuteExternalApplication(extractor1, extractor2, outputPath, exePath, title, ref taskTime, ref exitCode); }); // wait for task to complete exteranlTask.Wait(); Console.WriteLine(string.Format("Clpping process for task {0} exited with exit code {1}:", taskMessage.ID.ToString(), exitCode.ToString())); switch (exteranlTask.Status) { case TaskStatus.Faulted: receipt = AcknowledgementReceipt.FAILED; break; case TaskStatus.Canceled: receipt = AcknowledgementReceipt.CANCELED; break; case TaskStatus.RanToCompletion: { if (exitCode == 0 || exitCode == -529697949) { receipt = AcknowledgementReceipt.SUCCESS; } else if (exitCode == -1073741510) { receipt = AcknowledgementReceipt.CANCELED; } else { receipt = AcknowledgementReceipt.FAILED; } break; } } } // send the acknowledgement return new AcknowledgementMessage(taskMessage.ID, taskMessage.Description, taskTime, receipt); })); }
/// <summary> /// Perform the exceution of job, Handles "BeginJobMessage" message /// </summary> /// <param name="job"></param> private void HandleJobExecute(JobStartedMessage job) { _myJob = job; // use pipeto to handle async call from caller ( taskexecuter ) _taskExecuter.ExecuteTask(job).PipeTo(Self, Sender); }