publishingService.cs :  » Content-Management-Systems-CMS » umbraco » presentation » umbracoBase » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » umbraco 
umbraco » presentation » umbracoBase » publishingService.cs
using System;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Web;
using System.Xml;

using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;

namespace umbraco.presentation{
    /// <summary>
    /// Summary description for publishingService.
    /// </summary>
    public class publishingService
    {
        private static Hashtable scheduledTaskTimes = new Hashtable();
        private static bool isPublishingRunning = false;

        public static void CheckPublishing(object sender)
        {
            // don't run the publishing routines if Umbraco hasn't been configured yet
            if (!GlobalSettings.Configured)
                return;
            if (isPublishingRunning)
                return;
            isPublishingRunning = true;
            try
            {
                // DO not run publishing if content is re-loading
                if (!content.Instance.isInitializing)
                {
                    foreach (Document d in Document.GetDocumentsForRelease())
                    {
                        try
                        {
                            d.HttpContext = (HttpContext)sender;

                            d.ReleaseDate = DateTime.MinValue; //new DateTime(1, 1, 1); // Causes release date to be null

                            d.Publish(d.User);
                            library.UpdateDocumentCache(d.Id);

                        }
                        catch (Exception ee)
                        {
                            Log.Add(
                                LogTypes.Error,
                                BusinessLogic.User.GetUser(0),
                                d.Id,
                                string.Format("Error publishing node: {0}", ee));
                        }
                    }

                    foreach (Document d in Document.GetDocumentsForExpiration())
                    {
                        try
                        {
                            d.HttpContext = (HttpContext)sender;
                            d.ExpireDate = DateTime.MinValue;
                            // No check on publish state in the cache refresher as we could be in a load balanced environment!
                            library.UnPublishSingleNode(d.Id);
                            d.UnPublish();
                        }
                        catch (Exception ee)
                        {
                            Log.Add(
                                LogTypes.Error,
                                BusinessLogic.User.GetUser(0),
                                d.Id,
                                string.Format("Error unpublishing node: {0}", ee));

                        }
                    }
                }

                // run scheduled url tasks
                try
                {
                    XmlNode scheduledTasks = UmbracoSettings.ScheduledTasks;
                    if (scheduledTasks != null)
                    {
                        XmlNodeList tasks = scheduledTasks.SelectNodes("./task");
                        if (tasks != null)
                        {
                            foreach (XmlNode task in tasks)
                            {
                                bool runTask = false;
                                if (!scheduledTaskTimes.ContainsKey(task.Attributes.GetNamedItem("alias").Value))
                                {
                                    runTask = true;
                                    scheduledTaskTimes.Add(task.Attributes.GetNamedItem("alias").Value, DateTime.Now);
                                }
                                // Add 1 second to timespan to compensate for differencies in timer
                                else if (
                                    new TimeSpan(DateTime.Now.Ticks -
                                                 ((DateTime)scheduledTaskTimes[task.Attributes.GetNamedItem("alias").Value]).Ticks).TotalSeconds +
                                    1 >=
                                    int.Parse(task.Attributes.GetNamedItem("interval").Value))
                                {
                                    runTask = true;
                                    scheduledTaskTimes[task.Attributes.GetNamedItem("alias").Value] = DateTime.Now;
                                }

                                if (runTask)
                                {
                                    bool taskResult = getTaskByHttp(task.Attributes.GetNamedItem("url").Value);
                                    if (bool.Parse(task.Attributes.GetNamedItem("log").Value))
                                        Log.Add(LogTypes.ScheduledTask, User.GetUser(0), -1,
                                                string.Format("{0} has been called with response: {1}",
                                                              task.Attributes.GetNamedItem("alias").Value, taskResult));
                                }
                            }
                        }
                    }
                }
                catch (Exception ee)
                {
                    Log.Add(LogTypes.Error, User.GetUser(0), -1,
                        string.Format("Error executing scheduled task: {0}", ee));
                }
            }
            catch (Exception x)
            {
                Debug.WriteLine(x);
            }
            finally
            {
                isPublishingRunning = false;
            }
        }

        private static bool getTaskByHttp(string url)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = null;
            try
            {
                myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    myHttpWebResponse.Close();
                    return true;
                }
                else
                {
                    myHttpWebResponse.Close();
                    return false;
                }
            }
            catch
            {
            }
            finally
            {
                // Release the HttpWebResponse Resource.
                if (myHttpWebResponse != null)
                    myHttpWebResponse.Close();
            }

            return false;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.