public void Build(string[] arguments)
        {
            try
            {
                //check if another instance is running - if so exit
                ExecutionLogLogger.SetExecutionStartTime(DateTime.Now);
                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 (OutputInstructionProcessor.Initialize())
                {
                    Log.Info("Instruction processor processed instructions from the previous exection. Exiting the application without executing a new run.");
                    return;
                }

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

                // First decide if a full run should be enforced.
                var lastSuccessfulRun = FeedRunService.GetLastSuccessfulRun(FeedId);
                var enforceFullRun    = ShouldEnforceFullRun(arguments, lastSuccessfulRun);
                var isPseudoFullRun   = false;
                // If a full run is being enforced, but there is an existing successful run, execute an incremental run, where we still send products
                // that haven't been modified/touched by rule changes. This is necessary because a product expires in 30 days in Google if no updates
                // about the product is sent. The solution we came up with regarding this behavior is having 2 schedules for this feed generator, where one will run
                // only once a week and enforce a "full run". If we're in such a pseudo full run, then we'll execute an incremental run, while ensuring that unmodified
                // product data still gets passed.
                if (enforceFullRun && lastSuccessfulRun != null)
                {
                    isPseudoFullRun = true;
                }

                // Next, need to decide on the effective start time.
                _currentRun = FeedRunService.GetOrCreateActiveFeedRun(FeedId, enforceFullRun && !isPseudoFullRun, AllowIncrementalRuns);
                DateTime?fromTime           = null;
                DateTime?effectiveStartTime = null;
                // Only set these values if we aren't in a "real" full run
                if (!(enforceFullRun && !isPseudoFullRun))
                {
                    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.AddHours(-IncrementalRunBufferTimeLength);
                    }
                }

                _effectiveExecutionEndTime = DateTime.Now;

                Runner.Initialize(ExecutionLogLogger, GooglePlaFeedId, _currentRun.FeedRunType == FeedRunType.Incremental, fromTime, effectiveStartTime, _effectiveExecutionEndTime, isPseudoFullRun);
                ExecutionLogLogger = Runner.Execute();

                // Ensure that we kill the watcher here as well
                if (ExecutionLogLogger.HasError)
                {
                    Log.Error("Execution failed!!! Exiting application without further processing.");
                    HandleExit();
                    return;
                }

                HandleSuccess();

                var elapsedTime = DateTime.Now - ExecutionLogLogger.GetExecutionStartTime();
                Log.InfoFormat("Execution completed in {0}", elapsedTime.ToString(@"dd\.hh\:mm\:ss"));
            }
            catch (Exception e)
            {
                Log.Error("Execution failed!!!", e);
                HandleExit();
            }
        }
Exemple #2
0
        public void Build(string[] args)
        {
            try
            {
                //check if another instance is running - if so exit
                ExecutionLogLogger.SetExecutionStartTime(DateTime.Now);
                ExecutionLogLoggerSecondary.SetExecutionStartTime(DateTime.Now);
                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;
                }

                // First check if the "done" file is there. If so, skip job execution and move to file-related steps
                if (HasDoneFile())
                {
                    Log.Info("Done file exists at the drop folder. Skipping job execution and moving to file operations.");
                    ExecuteFileOperations();
                    return;
                }

                Log.Info("Execution started!");
                var   feed          = FeedService.GetFeed(FeedId);
                IFeed secondaryFeed = null;
                if (RunMode == RunMode.PrimaryAndSecondary)
                {
                    secondaryFeed = FeedService.GetFeed(FeedIdSecondary);
                }

                var allowPrimary   = !feed.IsPaused;
                var allowSecondary = secondaryFeed != null && !secondaryFeed.IsPaused;
                if (!allowPrimary && !allowSecondary)
                {
                    Log.InfoFormat("Both primary {0} and secondary {1} feeds are paused. Exiting application without any processing.", feed.Name, secondaryFeed.Name);
                    return;
                }

                // If one of the feeds is paused, notify the user and move on
                if (!allowPrimary)
                {
                    Log.InfoFormat("The primary feed {0} is in paused state. Skipping primary feed.", feed.Name);
                }

                if (RunMode == RunMode.PrimaryAndSecondary && !allowSecondary)
                {
                    Log.InfoFormat("The secondary feed {0} is in paused state. Skipping secondary feed.", secondaryFeed.Name);
                }

                // First clean all the files inside the output folder (in case the previous run failed and left out old files behind.)
                RemoveOldFiles();

                _currentRun = FeedRunService.GetOrCreateActiveFeedRun(feed.FeedId, true, false);
                int?secondaryRunId = null;
                if (RunMode == RunMode.PrimaryAndSecondary)
                {
                    _currentSecondaryRun = FeedRunService.GetOrCreateActiveFeedRun(secondaryFeed.FeedId, true, false);
                    secondaryRunId       = _currentSecondaryRun.FeedId;
                }

                _effectiveExecutionEndTime = DateTime.Now;
                Runner.Initialize(ExecutionLogLogger, ExecutionLogLoggerSecondary, _currentRun.FeedId, secondaryRunId);
                var results = Runner.Execute().ToList();
                ExecutionLogLogger = results[0];
                if (RunMode == RunMode.PrimaryAndSecondary)
                {
                    ExecutionLogLoggerSecondary = results[1];
                }

                if (ExecutionLogLogger.HasError || ExecutionLogLoggerSecondary.HasError)
                {
                    Log.Error("Execution failed!!! Exiting application without moving any files.");
                    HandleExit();
                    return;
                }

                HandleSuccess();

                var elapsedTime = DateTime.Now - ExecutionLogLogger.GetExecutionStartTime();
                Log.InfoFormat("Execution completed in {0}", elapsedTime.ToString(@"dd\.hh\:mm\:ss"));
            }
            catch (Exception e)
            {
                Log.Error("Execution failed!!!", e);
                HandleExit();
            }
        }