/// <summary>
        /// Async helper method, grabs the Queue that is set to be "running" for this application and processes.
        /// </summary>
        private static void RunSlugGenerationQueueItem()
        {
            //EventLogProvider.LogInformation("DynamicRouteTesting", "AsyncBuildStart", eventDescription: DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
            // Get the current thread ID and the item to run
            SlugGenerationQueueInfo ItemToRun = SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
                                                .WhereEquals("SlugGenerationQueueRunning", 1)
                                                .WhereEquals("SlugGenerationQueueApplicationID", SystemHelper.ApplicationIdentifier)
                                                .WhereEmpty("SlugGenerationQueueThreadID")
                                                .FirstOrDefault();

            if (ItemToRun == null)
            {
                return;
            }

            // Update item with thread and times
            ItemToRun.SlugGenerationQueueThreadID = Thread.CurrentThread.ManagedThreadId;
            ItemToRun.SlugGenerationQueueStarted  = DateTime.Now;
            ItemToRun.SetValue("SlugGenerationQueueErrors", null);
            ItemToRun.SetValue("SlugGenerationQueueEnded", null);
            SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);

            // Get the NodeItem from the SlugGenerationQueueItem
            var      serializer = new XmlSerializer(typeof(NodeItem));
            NodeItem QueueItem;

            using (TextReader reader = new StringReader(ItemToRun.SlugGenerationQueueNodeItem))
            {
                QueueItem = (NodeItem)serializer.Deserialize(reader);
            }
            // Build and Save Items
            try
            {
                QueueItem.BuildChildren();
                QueueItem.SaveChanges();

                // If completed successfully, delete the item
                ItemToRun.Delete();

                // Now that we are 'finished' call the Check again to processes next item.
                CheckUrlSlugGenerationQueue();
            }
            catch (Exception ex)
            {
                ItemToRun.SlugGenerationQueueErrors  = EventLogProvider.GetExceptionLogMessage(ex);
                ItemToRun.SlugGenerationQueueRunning = false;
                ItemToRun.SlugGenerationQueueEnded   = DateTime.Now;
                SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);

                // Commit transaction so next check will see this change
                CommitTransaction(true);

                // Now that we are 'finished' call the Check again to processes next item.
                CheckUrlSlugGenerationQueue();
            }
            //EventLogProvider.LogInformation("DynamicRouteTesting", "AsyncBuildComplete", eventDescription: DateTime.Now.ToString()+" "+DateTime.Now.Millisecond.ToString());
        }
        /// <summary>
        /// Adds the NodeItem to the Url Slug Generation Queue so it can be handled asyncly in the order it's added.
        /// </summary>
        /// <param name="NodeItem">The Node Item</param>
        private static void QueueUpUrlSlugGeneration(NodeItem NodeItem)
        {
            // Add item to the Slug Generation Queue
            SlugGenerationQueueInfo NewQueue = new SlugGenerationQueueInfo()
            {
                SlugGenerationQueueNodeItem = SerializeObject <NodeItem>(NodeItem)
            };

            SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(NewQueue);

            // Run Queue checker
            CheckUrlSlugGenerationQueue();
        }
        /// <summary>
        /// Clears the QueueRUnning flag on any tasks that the thread doesn't actually exist (something happened and the thread failed or died without setting the Running to false
        /// </summary>
        public static void ClearStuckUrlGenerationTasks()
        {
            Process currentProcess = Process.GetCurrentProcess();

            // Set any threads by this application that shows it's running but the Thread doesn't actually exist.
            SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
            .WhereEquals("SlugGenerationQueueRunning", true)
            .WhereEquals("SlugGenerationQueueApplicationID", SystemHelper.ApplicationIdentifier)
            .WhereNotIn("SlugGenerationQueueThreadID", currentProcess.Threads.Cast <ProcessThread>().Select(x => x.Id).ToArray())
            .ForEachObject(x =>
            {
                x.SlugGenerationQueueRunning = false;
                SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(x);
            });
        }
        /// <summary>
        /// Runs the Generation on the given Slug Generation Queue, runs regardless of whether or not any other queues are running.
        /// </summary>
        /// <param name="SlugGenerationQueueID"></param>
        public static void RunSlugGenerationQueueItem(int SlugGenerationQueueID)
        {
            SlugGenerationQueueInfo ItemToRun = SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
                                                .WhereEquals("SlugGenerationQueueID", SlugGenerationQueueID)
                                                .FirstOrDefault();

            if (ItemToRun == null)
            {
                return;
            }

            // Update item with thread and times
            ItemToRun.SlugGenerationQueueThreadID      = Thread.CurrentThread.ManagedThreadId;
            ItemToRun.SlugGenerationQueueStarted       = DateTime.Now;
            ItemToRun.SlugGenerationQueueRunning       = true;
            ItemToRun.SlugGenerationQueueApplicationID = SystemHelper.ApplicationIdentifier;
            ItemToRun.SetValue("SlugGenerationQueueErrors", null);
            ItemToRun.SetValue("SlugGenerationQueueEnded", null);
            SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);

            // Get the NodeItem from the SlugGenerationQueueItem
            var      serializer = new XmlSerializer(typeof(NodeItem));
            NodeItem QueueItem;

            using (TextReader reader = new StringReader(ItemToRun.SlugGenerationQueueNodeItem))
            {
                QueueItem = (NodeItem)serializer.Deserialize(reader);
            }

            // Build and Save Items
            try
            {
                QueueItem.BuildChildren();
                QueueItem.SaveChanges();
            }
            catch (Exception ex)
            {
                ItemToRun.SlugGenerationQueueErrors  = EventLogProvider.GetExceptionLogMessage(ex);
                ItemToRun.SlugGenerationQueueRunning = false;
                ItemToRun.SlugGenerationQueueEnded   = DateTime.Now;
                SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);
            }
            // If completed successfully, delete the item
            ItemToRun.Delete();
        }
 /// <summary>
 /// Clears the QueueRUnning flag on any tasks that the thread doesn't actually exist (something happened and the thread failed or died without setting the Running to false
 /// </summary>
 public static void ClearStuckUrlGenerationTasks()
 {
     // if no live threads have dynamic routing and there are some that show running, clear them.
     if (SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
         .WhereEquals("SlugGenerationQueueRunning", true)
         .WhereEquals("SlugGenerationQueueApplicationID", SystemHelper.ApplicationIdentifier)
         .Count > 0 &&
         ThreadDebug.LiveThreadItems.Where(x => DataHelper.GetNotEmpty(x.MethodClassName, "").StartsWith("DynamicRouting", StringComparison.InvariantCultureIgnoreCase)).Count() == 0)
     {
         SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
         .WhereEquals("SlugGenerationQueueRunning", true)
         .WhereEquals("SlugGenerationQueueApplicationID", SystemHelper.ApplicationIdentifier)
         .ForEachObject(x =>
         {
             x.SlugGenerationQueueRunning = false;
             x.SetValue("SlugGenerationQueueThreadID", null);
             x.SetValue("SlugGenerationQueueApplicationID", null);
             SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(x);
         });
     }
 }
Example #6
0
 /// <summary>
 /// Updates the object using appropriate provider.
 /// </summary>
 protected override void SetObject()
 {
     SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(this);
 }
Example #7
0
 /// <summary>
 /// Deletes the object using appropriate provider.
 /// </summary>
 protected override void DeleteObject()
 {
     SlugGenerationQueueInfoProvider.DeleteSlugGenerationQueueInfo(this);
 }
        /// <summary>
        /// Runs the Generation on the given Slug Generation Queue, runs regardless of whether or not any other queues are running.
        /// </summary>
        /// <param name="SlugGenerationQueueID"></param>
        public static void RunSlugGenerationQueueItem(int SlugGenerationQueueID)
        {
            SlugGenerationQueueInfo ItemToRun = SlugGenerationQueueInfoProvider.GetSlugGenerationQueues()
                                                .WhereEquals("SlugGenerationQueueID", SlugGenerationQueueID)
                                                .FirstOrDefault();

            if (ItemToRun == null)
            {
                return;
            }

            // Update item with thread and times
            ItemToRun.SlugGenerationQueueThreadID      = Thread.CurrentThread.ManagedThreadId;
            ItemToRun.SlugGenerationQueueStarted       = DateTime.Now;
            ItemToRun.SlugGenerationQueueRunning       = true;
            ItemToRun.SlugGenerationQueueApplicationID = SystemHelper.ApplicationIdentifier;
            ItemToRun.SetValue("SlugGenerationQueueErrors", null);
            ItemToRun.SetValue("SlugGenerationQueueEnded", null);
            SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);

            // Get the NodeItem from the SlugGenerationQueueItem
            var      serializer = new XmlSerializer(typeof(NodeItem));
            NodeItem QueueItem;

            using (TextReader reader = new StringReader(ItemToRun.SlugGenerationQueueNodeItem))
            {
                QueueItem = (NodeItem)serializer.Deserialize(reader);
            }

            // Build and Save Items
            try
            {
                QueueItem.BuildChildren();
                if (ErrorOnConflict() && QueueItem.ConflictsExist())
                {
                    ItemToRun.SlugGenerationQueueErrors = $"The Following Conflicts were found:\n\r{string.Join("\n\r", QueueItem.GetConflictItems())}\n\rPlease Correct and re-run queue item.";
                    ItemToRun.SlugGenerationQueueEnded  = DateTime.Now;
                    ItemToRun.SetValue("SlugGenerationQueueThreadID", null);
                    ItemToRun.SetValue("SlugGenerationQueueApplicationID", null);
                    ItemToRun.SlugGenerationQueueRunning = false;
                    SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);
                    return;
                }
                else
                {
                    QueueItem.SaveChanges();
                    // If completed successfully, delete the item
                    ItemToRun.Delete();
                }
            }
            catch (Exception ex)
            {
                ItemToRun.SlugGenerationQueueErrors  = EventLogProvider.GetExceptionLogMessage(ex);
                ItemToRun.SlugGenerationQueueRunning = false;
                ItemToRun.SlugGenerationQueueEnded   = DateTime.Now;
                SlugGenerationQueueInfoProvider.SetSlugGenerationQueueInfo(ItemToRun);
            }

            // Now that we are 'finished' call the Check again to processes next item.
            CheckUrlSlugGenerationQueue();
        }