Disabling EPiServer Scheduled Jobs programmatically in non production environment

icon of user profile

Scenario: You just have copied the Production Database to your development server or to the testservers but you don’t want that some important jobs to be activated and running in the background on a non production environment.

Solution: Automatically disable EPiServer Scheduled Jobs programmatically in the initualization.

Following Māris Krivtežs blog post which describes greatly how to work with scheduled jobs. http://marisks.net/2015/05/04/episerver-working-with-scheduled-jobs-programmatically/

My code:

    [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule ))]
    public class WebInit: EPiServer.Framework.IInitializableModule
    {
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            SetScheduleJobs();
        }
     }

        private void SetScheduleJobs()
        {
            if (!IsProdServer)
            {
                var rep = new ScheduledJobRepository();

                var job = rep.Get(typeof(SiteMapBuilderJob));
                DisableJob(job, rep);

                job = rep.Get(typeof(GenerateThumbnails));
                DisableJob(job, rep);

                job = rep.Get(typeof(UserExporttoSalesforce));
                DisableJob(job, rep);
            }
        }

        public static void DisableJob(ScheduledJob job, ScheduledJobRepository rep)
        {            
            if (job!=null && job.IsEnabled)
            {
                job.IsEnabled = false;
                rep.Save(job);
            }
        }

How to determind production server or not:

        public bool IsProdServer
        {
            get
            {
                //saved in web.config thru deployment system eg Octopus
                var environment = ConfigurationManager.AppSettings["Environment"];
                if (!String.IsNullOrEmpty(environment))
                {
                    if (environment.ToLower().Equals("production"))
                        return true;
                }
                //Check name of server
                string ProdMachineName = ConfigurationManager.AppSettings["ProdMachineName"];
                if (!String.IsNullOrEmpty(ProdMachineName))
                {
                    if (Environment.MachineName.ToLower().IndexOf(ProdMachineName) > -1)
                    { return true; }
                }
                return false;
            }
        }

Explanation: If you are using Config Transforms and any automatic deployment system, in my case Octopus, the “Environment” appsettings is set in the transform config. eg

 <add key="Environment" value="#{Octopus.Environment.Name}" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" />

A second possibility is to check the MachineName.

More about scheduled jobs: http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-CMS/8/Scheduled-jobs/Scheduled-jobs/