private void UpdateCountersInReport(ExecutionInformation executionInformation, RunType runType)
        {
            _reportInformation.NumberOfRecordsPerFile = _executionInformation.RecordsPerFile;
            if (runType == RunType.Full)
            {
                _reportInformation.NumberOfNewRecords = _executionInformation.TotalRecordsNew + _executionInformation.TotalRecordsModified;
            }
            else
            {
                _reportInformation.NumberOfNewRecords      = _executionInformation.TotalRecordsNew;
                _reportInformation.NumberOfModifiedRecords = _executionInformation.TotalRecordsModified;
            }
            _reportInformation.NumberOfDeletedRecords = _executionInformation.TotalRecordsDeleted;
            _reportInformation.NumberOfErrorRecords   = _executionInformation.TotalRecordsErrored;
            var messages = _executionInformation.GetCustomMessages();

            for (int i = 0; i < messages.Length && i < ReportMaxNumberOfMessages; i++)
            {
                _reportInformation.CustomMessages.Add(messages[i]);
                if (i + 1 == ReportMaxNumberOfMessages)
                {
                    _reportInformation.CustomMessages.Add("See logs for more messages.");
                }
            }
        }
 public void Initialize(RunType runType, DateTime?effectiveFromTime, DateTime?effectiveToTime)
 {
     _executionInformation = new ExecutionInformation
     {
         RecordsPerFile = NumberOfRecordsPerBatch
     };
     _runType           = runType;
     _effectiveFromTime = effectiveFromTime;
     _effectiveToTime   = effectiveToTime;
 }
        public void Build(RunType requestedRunType, DateTime?effectiveStartTime = null, DateTime?effectiveEndTime = null)
        {
            try
            {
                NewRelic.Api.Agent.NewRelic.AddCustomParameter("FeedId", FeedId);
                // start report creation
                _reportInformation = new ReportInformation(FeedId)
                {
                    ExecutionStartTime = DateTime.Now,
                    RunType            = requestedRunType,
                    EffectiveEndTime   = effectiveEndTime
                };

                //check if another instance is running - if so exit
                var isProcessRunning = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1;
                if (isProcessRunning)
                {
                    Log.Info("Another instance was running, so exiting the application without any processing.");
                    return;
                }

                Log.Info("Execution started!");
                if (!Runner.IsReady())
                {
                    Log.Info("Runner is not ready for a new run. Exiting the application without executing a new run.");
                    return;
                }

                var feed = FeedService.GetFeed(FeedId);
                _reportInformation.FeedName = feed.Name;
                if (feed.IsPaused)
                {
                    Log.InfoFormat("{0} feed is paused. Exiting application without any processing.", feed.Name);
                    _reportInformation.CustomMessages.Add("Feed is paused. No processing done.");
                    return;
                }

                // First decide if a full run should be enforced.
                var lastSuccessfulRun = FeedRunService.GetLastSuccessfulRun(FeedId);
                var enforceFullRun    = ShouldEnforceFullRun(requestedRunType, lastSuccessfulRun);

                // If incremental run requested, but full run required first (unless forced)
                // then abort as the incremental might be configured for different output
                if (requestedRunType == RunType.Incremental && enforceFullRun)
                {
                    Log.InfoFormat("{0} feed must execute a full run first. Exiting application without any processing.", feed.Name);
                    _reportInformation.CustomMessages.Add("Must execute a full run first. No processing done.");
                    return;
                }

                // Next, need to decide on the effective start time.
                _currentRun = FeedRunService.GetOrCreateActiveFeedRun(FeedId, enforceFullRun, AllowIncrementalRuns);
                _reportInformation.FeedRunId = _currentRun.FeedRunId;
                DateTime?fromTime = null;
                // Only set these values if we aren't in a "real" full run
                if (!enforceFullRun && requestedRunType != RunType.OnDemand)
                {
                    fromTime = GetFromTime(_currentRun);
                    // Effective start time is used for callibrating the fromTime to a previous point in time, in case this is needed due to the execution
                    // sequence/timinig of other processes that impact the data that the inventory feed depends on. For example, if results of an inventory-related
                    // process takes 2 hours to get replicated down to the catalogue/website, where as the inventory data has already been updated in Bronte at
                    // the time of execution AND business has updated rules related to the feed in the past 15 minutes, then gathering the incremental data as
                    // if the run started two hours ago but applying rule changes using "now" as the reference point will yield more "accurate" results.
                    effectiveStartTime = fromTime;
                    if (_currentRun.FeedRunType == FeedRunType.Incremental)
                    {
                        effectiveStartTime = fromTime.Value.AddMinutes(-IncrementalRunBufferTimeLength);
                    }
                }

                _reportInformation.EffectiveStartTime = effectiveStartTime;
                _effectiveExecutionEndTime            = DateTime.Now;

                var runType = _currentRun.FeedRunType == FeedRunType.Incremental ? RunType.Incremental : requestedRunType;
                Runner.Initialize(
                    runType,
                    effectiveStartTime,
                    effectiveEndTime);
                _executionInformation = Runner.Execute();
                UpdateCountersInReport(_executionInformation, runType);

                // Ensure that we kill the watcher here as well
                if (_executionInformation.HasError)
                {
                    _reportInformation.HasErrors = true;
                    LoggingHelper.Error("Execution failed!!! Exiting application without further processing.", Log);
                    HandleExit();
                    return;
                }

                _reportInformation.ExecutionEndTime = DateTime.Now;
                HandleSuccess();

                var elapsedTime = _reportInformation.ExecutionEndTime.Value - _reportInformation.ExecutionStartTime;
                Log.InfoFormat("Execution completed in {0}", elapsedTime.ToString(@"dd\.hh\:mm\:ss"));
            }
            catch (Exception e)
            {
                LoggingHelper.Error(e, "Execution failed!!!", Log);
                _reportInformation.CustomMessages.Add("Exception during execution. See logs for details.");
                HandleExit();
            }
        }