Invoke Sitecore Scheduling Agent Programmatically

Sitecore provides facility to run scheduled jobs at specified regular interval like DatabaseAgent, UrlAgent, etc. We created our own agent to make scheduled publishing. So, this agent is executed every one hour and starts pending publishing, which can take several minutes say 20-30 minutes.

This can be achieved by creating a scheduling agent in web.config as below:
    <agent name="ScheduledPublish" type="SitecoreTactics.Tasks.StartPublish" method="StartPublish" interval="01:00:00" />
Here, every 1 hour, Sitecore invokes the agent named ScheduledPublish and executes StartPublish method of SitecoreTactics.Tasks.PublishManager class.

Requirement to invoke Sitecore Agent

Now, we had a requirement to invoke our custom agent in between through a Web Page when user wants to do instant publish instead of waiting for next turn of its execution. Should we call our code from the web page itself or invoke the Agent directly programmatically? There was no clear idea which can be a better approach? Below are two approaches we worked on.

Wrong approach we chose earlier to execute code directly

Earlier, we called the PublishManager's StartPublish method directly from the web page, when user clicks on a button as below.
protected void btnStartPublish_Click(object sender, EventArgs e)
{
    SitecoreTactics.Tasks.PublishManager publisher = new SitecoreTactics.Tasks.PublishManager();
    publisher.StartPublish();
}
The limitation in the approach was, the code was executed by the thread of web page or a web request, which needed to be executed more than 30 minutes. But, after the Server's Script timeout (default 5 minutes), the execution was stopped every time and could not complete full job of 30+ minutes. But if same code was executed as a Sitecore Agent, it finished all execution, because Sitecore Agents are executed by separate ManagedThreadPools, which never has TimeOut until they finish job or Application ends.

Correct approach to invoke Sitecore Scheduling Agent

We searched a lot on Google about invoking Sitecore Agent. Later on, Reflector came to our help. Using it we found the code from where Sitecore invokes its agents and we implemented the same code as below:
protected void btnStartPublish_Click(object sender, EventArgs e)
{
 foreach (XmlNode node in Factory.GetConfigNodes("scheduling/agent"))
 {
  object obj = Factory.CreateObject(node, true);
  string name = XmlUtil.GetAttribute("name", node);
 
  if (name == "ScheduledPublish") // Scheduling Agent Name
  {
   string method = XmlUtil.GetAttribute("method", node);
 
   JobOptions options = new JobOptions(name, "", "", obj, method);
   options.AtomicExecution = true; // Allow multiple instances of the agent running simultaneously
   JobManager.Start(options);
 
   break;
  }
 }
}
Now, we are not worried for executing agents's code. Agent's process needs to be executed for one hour, two hours, ten hours? No worries, we can now call Sitecore Jobs programmatically!!

Custom Google Search Engine for Sitecore Technical Blogs

One day I was thinking that how difficult it is to find out technical help from thousands of blogs written on net. Finding best blogs out of them is really difficult.

There are many ways to make a collection of Sitecore Technical Blogs like Feedly, NewsBlur, InoReader, etc. But, here I have prepared a Custom Google Search Engine which has pre-built indexes for good Sitecore technical blogs and Sitecore Community.

How to access this Custom Google Search Engine for Sitecore?

Open: http://goo.gl/YzZuCm
OR
https://www.google.com/cse/publicurl?cx=003865006081614653083:n809uqjt03c

This is a Google's custom Search Engine, which gives results from selected different Sitecore bloggers, Sitecore Communities, etc.
 

Enjoy searching Sitecore stuffs easily and quickly!! 

gZip Compression removes pre-existing vary header

We had a requirement to modify Vary Response Header to 'User-Agent' for mobile site SEO for google. Have you faced a situation that your changed Vary header is not getting reflected on the Response Headers on ASP.NET webpage?

We tried to modify the header from Page_Load event of a webpage. but when page is loaded, my HttpWatch/Fiddler is not showing Vary header as 'User-Agent', it is still showing 'Accept-Encoding' instead. Checkout my code:
using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 

public partial class _Default : System.Web.UI.Page 
{ 
   protected void Page_Load(object sender, EventArgs e) 
   { 
      Response.Write("Hello"); 

      // Modify Vary Response Header
      Response.AppendHeader("Vary", "User-Agent"); 
   } 
}
Above code failed so we also tried to append these headers at Page_LoadComplete and context_EndRequest event(last event in page life cycle) assuming that the headers were tampered in between (Between Page_Load and context_EndRequest). See below snap, showing our code not working, means showing Vary as 'Accept-Encoding' instead of 'User-Agent'. So, something strange was happening here.


How we found the cause and the solution

One thing was sure that the headers are overwritten from IIS level, because our modified headers set in context_EndRequest event(last event in page life cycle) were also getting overwritten. After spending few hours, I came to know that dynamic compression module from IIS overwrites the Vary header to 'Accept-Encoding'. Disabling this module solved our problem. Such a nasty bug it is!!

This issue is already addressed by an official patch to IIS. We can download the HotFix from Microsoft - http://support.microsoft.com/kb/2877816

After installing this HotFix, this issue is resolved!!



We can now play with Vary header along with Dynamic Content Compression module!!