/// <summary>
        /// Do some work. This is where the meat of the processing is done
        /// </summary>
        public async Task <JobResultModel> DoWorkAsync(string jobId, JobParametersModel work,
                                                       CancellationToken cancellationToken)
        {
            // here's an HttpClient if you need one
            var httpClient = _httpClientFactory.CreateClient();

            var next   = work.SeedData;
            var result = new JobResultModel();

            var sw = new Stopwatch();

            sw.Start();
            for (ulong i = 0; i < work.Iterations; ++i)
            {
                next = unchecked (next * 1103515245 + 12345);
                result.CalculatedResult = next / 65536 % 32768;

                await Task.Delay(1000,
                                 cancellationToken).ConfigureAwait(false);        // simulate long-running task.

                // make sure we only update status once a second
                if (sw.ElapsedMilliseconds >= 1000)
                {
                    sw.Restart();
                    await _computationJobStatus.UpdateJobProgressInformationAsync(
                        jobId, $"Current result: {result.CalculatedResult}",
                        i / (double)work.Iterations).ConfigureAwait(false);
                }
            }

            await _computationJobStatus.UpdateJobProgressInformationAsync(
                jobId, $"Done", 1.0).ConfigureAwait(false);

            return(result);
        }
        /// <summary>
        /// Transient method via IQueuedBackgroundService
        /// </summary>
        public async Task <JobCreatedModel> PostWorkItemAsync(JobParametersModel jobParameters)
        {
            var jobId = await _jobStatusService.CreateJobAsync(jobParameters).ConfigureAwait(false);

            _queue.Enqueue(new JobQueueItem {
                JobId = jobId, JobParameters = jobParameters
            });
            _signal.Release();             // signal for background service to start working on the job
            return(new JobCreatedModel {
                JobId = jobId, QueuePosition = _queue.Count
            });
        }
        public async Task <string> CreateJobAsync(JobParametersModel jobParameters)
        {
            // find a jobId that isn't in use (this may be pointless, but could you imagine?)
            string jobId;

            do
            {
                jobId = Guid.NewGuid().ToString();
            } while (await _jobStorage.ExistsAsync(DatabaseKey, jobId).ConfigureAwait(false));

            await WriteAsync(new JobModel
            {
                JobId          = jobId,
                StartTime      = DateTime.UtcNow,
                Status         = JobStatus.Pending,
                WorkParameters = jobParameters
            }).ConfigureAwait(false);

            return(jobId);
        }
Example #4
0
 public async Task <IActionResult> BeginComputation([FromBody] JobParametersModel obj)
 {
     return(Accepted(
                await _queuedBackgroundService.PostWorkItemAsync(obj).ConfigureAwait(false)));
 }