Beispiel #1
0
        public async Task <IActionResult> Put(Guid uuid, [FromBody] MapTask task)
        {
            var jobsDictionary = await this.stateManager_.GetOrAddAsync <IReliableDictionary <Guid, JobObject> >("jobsDictionary");

            var completeJobsQueue = await this.stateManager_.GetOrAddAsync <IReliableConcurrentQueue <string> >("completeJobsQueue");

            var allTasks = await this.stateManager_.GetOrAddAsync <IReliableDictionary <Guid, MapTask> >("allTasks");

            using (ITransaction tx = this.stateManager_.CreateTransaction())
            {
                var jobConditionalValue = await jobsDictionary.TryGetValueAsync(tx, uuid);

                if (jobConditionalValue.HasValue)
                {
                    JobObject job = jobConditionalValue.Value;
                    job.SetTaskCompleted(task.Uuid);

                    if (job.IsComplete)
                    {
                        // Job is complete, remove it, send the result to FrontEndSvc for now
                        var removeConditional = await jobsDictionary.TryRemoveAsync(tx, job.Uuid);

                        if (!removeConditional.HasValue)
                        {
                            // Retry, I guess?
                        }

                        var counts = new List <Dictionary <string, int> >();

                        // Current task's Output field is null when taken from allTasks dictionary
                        foreach (var taskUuid in job.Tasks.Keys)
                        {
                            MapTask t = (await allTasks.TryRemoveAsync(tx, taskUuid)).Value;

                            counts.Add(t.Output ?? task.Output);
                        }

                        string serialized = JsonConvert.SerializeObject(counts);
                        await completeJobsQueue.EnqueueAsync(tx, serialized);
                    }
                    else
                    {
                        task.State = MapTask.StateType.Completed;
                        await jobsDictionary.SetAsync(tx, job.Uuid, job);

                        await allTasks.SetAsync(tx, task.Uuid, task);
                    }
                }

                await tx.CommitAsync();
            }
            return(new OkResult());
        }