• Decrease Text SizeIncrease Text Size

Setup of Custom Scheduled task

Posted Date: 5/2/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size

Scheduled tasks are running in the master console.

Please see new updated way to setup a scheduled task



To create Scheduled tasks you need to follow the next steps.
1. Copy the SQL statement, update appropriate values, and run it on your Master database.
2. In this example cpCollection only contains one property but it can have as many as you need to take parameters for your task.


INSERT INTO cpsys_Jobs (JobId, SystemName, Name, Description, Properties, Roles, IsEnabled, CreateDate, ModifyDate)
VALUES (NEWID(), 'SYSTEM_NAME_OF_YOUR_TASK', 'NAME_OF_YOUR_TASK', 'DESCRIPTION_OF_YOUR_TASK.', 
	'<cpCollection>
		  <group id="General" name="General">
				<property id="WebSiteId">
					<value/>
					<attribute name="WebSite Id">
						<control type="Centralpoint.Web.UI.Controls.CpTextBox"/>
						<validator id="CpRequiredFieldValidator" type="Centralpoint.Web.UI.Controls.CpRequiredFieldValidator"/>
					</attribute>
				</property>
		  </group>
	</cpCollection>', NULL, 1, GETDATE(), NULL)
	  

You will need to update:
SYSTEM_NAME_OF_YOUR_TASK: with SystemName of the task(must be unique).
NAME_OF_YOUR_TASK: with the name of this task.
DESCRIPTION_OF_YOUR_TASK: with a description of the task.
XML cp Collection can contain additional controls. If you need to take any additional properties.
3. Create my_ScheduledTasks.cs file in \\SERVER_IP\Centralpoint\Master\Root\App_Code folder and copy the code below into your file.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Centralpoint;
using Centralpoint.Web;
using Centralpoint.Web.Cms;
using Centralpoint.Web.UI;
using Centralpoint.Web.UI.Controls;
using Centralpoint.Web.UI.Console;
using Centralpoint.Web.Site;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Centralpoint.Master
{
    /// <remarks>
    /// This class is used to execute scripts.
    /// </remarks>
    public partial class ScheduledTasks
    {
        /// <summary>
        /// This method executes custom scheduled tasks.
        /// </summary>
        /// <param name="systemName">The lowered value of the system name of the scheduled job.
        /// <param name="collection">A collection of properties entered for this instance of the scheduled task.
        /// <param name="scheduledTaskId">The id of this instance of the scheduled task.
        static partial void ExecuteMyTask(string systemName, CpCollection collection, Guid scheduledTaskId)
        {
            HttpContext.Current.Trace.Write("Executing MyTask", systemName);
            switch (systemName.ToLower())
            {
                //system_name_of_your_task in lower case.
                case "system_name_of_your_task":
                    My_System_Name_Of_Your_Task(collection, scheduledTaskId);
                    break;
                default:
                    return;
            }
        }

        /// <summary>
        /// This method executes a custom scheduled task.
        /// </summary>
        /// <param name="collection">A collection of properties entered for this instance of the scheduled task.
        /// <param name="scheduledTaskId">The id of this instance of the scheduled task.
        private static void My_System_Name_Of_Your_Task(CpCollection collection, Guid scheduledTaskId)
        {
            my_ScheduledTasks.CustomJob customJob = new my_ScheduledTasks.CustomJob(collection, scheduledTaskId);
            customJob.Execute();
        }
    }

    namespace my_ScheduledTasks
    {
        public class CustomJob
        {
            private CpCollection Collection { get; set; }
            private Guid ScheduledTaskId { get; set; }
            private Centralpoint.Master.Common.WebSite WebSite { get; set; }
            private string ConnectionString { get; set; }
            private string WebSiteId { get; set; }

            /// <summary>
            /// This method provides the default constructor.
            /// </summary>
            public CustomJob(CpCollection collection, Guid scheduledTaskId)
            {
                this.Collection = collection;
                this.ScheduledTaskId = scheduledTaskId;
                this.WebSiteId = collection.Get("WebSiteId");
            }

            /// <summary>
            /// This method executes the custom job.
            /// </summary>
            public void Execute()
            {
                Process process = Centralpoint.Web.ProcessMonitor.StartProcess(Guid.NewGuid(), ProcessTypes.MyScheduledTask, "Starting My task...");
                try
                {
                    if (!this.WebSiteId.IsGuid())
                    {
                        process.Status = "Invalid GUID.";
                        throw new UnreportedException("Invalid GUID.");
                    }
                    this.WebSite = Common.WebSite.NewCached(new Guid(this.WebSiteId));
                    this.ConnectionString = this.WebSite.SelectConnectionString;

                    process.Status = "Scheduled task for '" + this.WebSite.Name + "' website was executed.";

                    Centralpoint.Web.ProcessMonitor.StopProcess(process, "My task Completed");
                }
                catch (Exception ex)
                {
                    process.Status = ex.Message;
                    process.Status = ex.StackTrace;
                    Centralpoint.Web.ProcessMonitor.StopProcess(process, "My task Failed");
                    Management.Error(ex, false);
                }
            }
        }
    }
}
	  


4. Navigate to your master console. then click on Content > Scheduled Tasks.
5. Click the 'New' button and enter a name for your Scheduled Tasks. Then from the Job selector select the name of your job. 5. Enter the Id of the website which you can find in the console Admin > Settings > Site Key into the WebSite Id attribute. 6. Update the Start Date to sometime in the future and click save.


* Centralpoint by default scheduled tasks set up to run every 30 min. you can verify that by navigating to \\SERVR_IP\Centralpoint\Agent CentralpointAgent.exe.config file appSettings 'Interval_Minutes' property.

Keywords: Scheduled task



Related Code Samples Records
Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.