public async Task <RemedyPollResult> PollAsync(DateTime fromUtc)
        {
            Stopwatch watch = new Stopwatch();

            var result = new RemedyPollResult(fromUtc);
            IEnumerable <OutputMapping1GetListValues> workItemsChanged = null;

            try { workItemsChanged = await _remedyService.GetRemedyChangedWorkItems(fromUtc); }
            catch (Exception err)
            {
                result.ProcessErrors.Add(new ProcessError()
                {
                    ErrorMessage = err.Message
                });
            }
            if (workItemsChanged != null && workItemsChanged.Any())
            {
                await ProcessWorkItemsChanged(workItemsChanged, result, fromUtc);
            }
            result.EndTimeUtc = result.RecordsProcesed.Any()
                ? result.RecordsProcesed.Max(x => x.Last_Modified_Date)
                : lastPollTimeUtc;

            _logger.Information($"Finished Polling Remedy in { watch.Elapsed.TotalSeconds}s");

            return(result);
        }
        private void SaveResult(RemedyPollResult result)
        {
            var filename = Path.Combine(_options.TempDirectory, $"{ResultFilePrefix}_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.log");

            using (StreamWriter file = File.CreateText(filename))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(file, result);
            }
        }
        private async Task ProcessWorkItemsChanged(IEnumerable <OutputMapping1GetListValues> workItemsChanged,
                                                   RemedyPollResult result, DateTime timestampUtc)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            int count = 0;

            foreach (var workItem in workItemsChanged)
            {
                _logger.Information("Processing workItemChanged for id {InstanceId}", workItem.InstanceId);

                // We can do the next line because this service will always be in the same time zone as Remedy
                DateTime lastModifiedDateUtc = workItem.Last_Modified_Date.ToUniversalTime();

                if (lastModifiedDateUtc <= timestampUtc)
                {
                    _logger.Warning($"WorkItem { workItem.WorkOrderID } has a last modified date less than or equal to our cutoff time, so ignoring ({ lastModifiedDateUtc } <= { timestampUtc }");
                    continue;
                }

                Exception error = null;
                try
                {
                    await TryProcessWorkItemChanged(workItem);
                }
                catch (Exception err)
                {
                    error = err;
                }

                if (error == null)
                {
                    result.RecordsProcesed.Add(workItem);
                }
                else
                {
                    result.ProcessErrors.Add(new ProcessError()
                    {
                        WorkItem     = workItem,
                        ErrorMessage = error.Message
                    });
                }
                count++;
            }
            TimeSpan avg = count > 0 ? watch.Elapsed / count : TimeSpan.Zero;

            _logger.Information($"Processed { count } work item changes in { watch.Elapsed }. Average = { avg.TotalMilliseconds }ms/record ");
        }