Exemplo n.º 1
0
        public async Task <ActionResult <SortJob> > EnqueueAndRunJob(int[] values)
        {
            var pendingJob = new SortJob(
                id: Guid.NewGuid(),
                status: SortJobStatus.Pending,
                duration: null,
                input: values,
                output: null);

            var completedJob = await _sortJobProcessor.Process(pendingJob);

            return(Ok(completedJob));
        }
        private Task Process()
        {
            while (_runBackGroundProcess)
            {
                var queue = new Dictionary <Guid, SortJob>();

                using (IServiceScope scope = _srvices.CreateScope())
                {
                    var scopedProcessingService = scope.ServiceProvider
                                                  .GetRequiredService <IBackgroundTaskQueue>();

                    queue = scopedProcessingService.Queue;
                }

                if (queue.Where(x => x.Value.Status == SortJobStatus.Pending).Any())
                {
                    KeyValuePair <Guid, SortJob> pendingJob = queue
                                                              .Where(x => x.Value.Status == SortJobStatus.Pending)
                                                              .FirstOrDefault();

                    if (pendingJob.Value is null)
                    {
                        continue;
                    }

                    queue[pendingJob.Key] = _sortJobProcessor.Process(pendingJob.Value).Result;
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogDebug("Sort Service starting");
            stoppingToken.Register(() =>
                                   _logger.LogDebug($" Sort Service background task is stopping."));

            while (!stoppingToken.IsCancellationRequested)
            {
                if (MemoryQueue.GetPendingQueue().Count > 0)
                {
                    foreach (SortJob pendingJob in MemoryQueue.GetPendingQueue())
                    {
                        SortJob completedJob = await _sortJobProcessor.Process(pendingJob);

                        MemoryQueue.UpdateQueue(completedJob);
                    }
                }
                await Task.Delay(5000, stoppingToken);
            }

            _logger.LogDebug("Sort Service background task is stopping.");
        }
Exemplo n.º 4
0
        public async Task <SortJob> Process(SortJob job)
        {
            var status = await _processor.Process(job);

            return(status);
        }
 private void ProcessJob(SortJob objJob)
 {
     _completedJobs.Add(_sortJobProcessor.Process(objJob).Result);
 }
Exemplo n.º 6
0
        private async void ProcessJob(SortJob job)
        {
            var completedJob = await _sortJobProcessor.Process(job);

            _sortJobsRepo[job.Id] = completedJob;
        }
Exemplo n.º 7
0
        public async Task DoWork(CancellationToken cancellationToken)
        {
            try
            {
                string jsonResponse = "";
                string filePath     = @"jobs.txt";

                while (!cancellationToken.IsCancellationRequested)
                {
                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (StreamReader sr = new StreamReader(fs))
                        {
                            var jsonData = sr.ReadToEnd();
                            // De-serialize to object or create new list
                            var jobList = JsonConvert.DeserializeObject <List <SortJob> >(jsonData)
                                          ?? new List <SortJob>();


                            foreach (SortJob sjob in jobList)
                            {
                                if (sjob.Status != SortJobStatus.Completed)
                                {
                                    await _sortJobProcessor.Process(sjob);

                                    sjob.Status = SortJobStatus.Completed;
                                }
                            }
                            jsonResponse = JsonConvert.SerializeObject(jobList);
                            sr.Close();
                            sr.Dispose();
                        }
                        fs.Close();
                        fs.Dispose();
                    }
                    using (FileStream fswrite = new FileStream(filePath, FileMode.Create))
                    {
                        using (var sw = new StreamWriter(fswrite))
                        {
                            var jobList = JsonConvert.DeserializeObject <List <SortJob> >(jsonResponse)
                                          ?? new List <SortJob>();
                            if (jobList.Count > 0)
                            {
                                sw.WriteLine(jsonResponse);
                            }
                            sw.Close();
                            sw.Dispose();
                        }

                        fswrite.Close();
                        fswrite.Dispose();
                    }

                    //System.IO.File.WriteAllText(@"jobs.txt", jsonResponse);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
            }
        }