Example #1
0
        public async Task <ScheduledJob> GetJobAsync(int jobId)
        {
            var job = await _scheduledJobs.GetJobAsync(jobId);

            return(job);
        }
Example #2
0
        private async Task StartTask()
        {
            while (_running)
            {
                try
                {
                    var job = await FindPendingJob();

                    if (job != null)
                    {
                        var state = ProcessorState.InProgress;
                        Total = _config.IsSP500
                                ? await CompanyService.GetSP500CountAsync()
                                : await CompanyService.GetTotalCountAsync();

                        await InitializeProcessor();

                        while (state == ProcessorState.InProgress && !job.IsFinished())
                        {
                            try
                            {
                                var companies = await FetchNextAsync(job.JobId);

                                if (companies == null || !companies.Any())
                                {
                                    state = ProcessorState.Completed;
                                }
                                else
                                {
                                    var tickers = companies.Select(c => c.Ticker).ToArray();

                                    await Execute(job, companies);

                                    await JobsService.UpdateProgressAsync(job.JobId, tickers, Total);

                                    Logger.Info(new ProcessorInfo
                                    {
                                        JobId       = job.JobId,
                                        JobType     = job.JobType,
                                        JobState    = job.Status,
                                        ProcessName = JobType.ToString()
                                    }, $"Processing Tickers: {tickers}");

                                    state = ProcessorState.InProgress;
                                    job   = await JobsService.GetJobAsync(job.JobId);
                                }
                            }
                            catch (Exception exception)
                            {
                                Logger.Error(new ProcessorInfo
                                {
                                    JobId       = job.JobId,
                                    JobType     = job.JobType,
                                    JobState    = JobStatus.Error,
                                    ProcessName = JobType.ToString()
                                }, $"{exception.Message}", exception);
                                state = ProcessorState.Error;
                            }
                        }

                        if (state == ProcessorState.Completed)
                        {
                            await FinalizeJob(job);


                            await JobsService.CompleteJobAsync(job.JobId);

                            Logger.Info(new ProcessorInfo
                            {
                                ProcessName = JobType.ToString(),
                                JobId       = job.JobId,
                                JobType     = ScheduledJobType.CalculateGlobalIndicators,
                                JobState    = JobStatus.Completed
                            }, "Job completed successfully");

                            await JobsService.ClearJobProgressAsync(job.JobId);
                        }

                        if (state == ProcessorState.Error)
                        {
                            job.Status = JobStatus.Error;
                            await JobsService.UpdateJobAsync(job);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(
                        new ProcessorInfo {
                        ProcessName = JobType.ToString()
                    },
                        ex.Message, ex);
                }

                Thread.Sleep(_config.Interval);
            }
        }