Last active
March 24, 2020 07:29
-
-
Save hawjeh/78b49a76ba86def16fe420faad7f47ba to your computer and use it in GitHub Desktop.
Sitefinity - Generic Task function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Ninject.Modules; | |
| using SitefinityWebApp.Tasks; | |
| namespace SitefinityWebApp.App_Start | |
| { | |
| public class DependencyMapping : NinjectModule | |
| { | |
| public override void Load() | |
| { | |
| this.Bind<IGenericTask>().To<GenericTask>(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using SitefinityWebApp.Utils; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Reflection; | |
| using Telerik.Sitefinity.Scheduling; | |
| using Telerik.Sitefinity.Scheduling.Model; | |
| namespace SitefinityWebApp.Tasks | |
| { | |
| public class GenericTask : IGenericTask | |
| { | |
| private readonly SchedulingManager _manager; | |
| public GenericTask() | |
| { | |
| _manager = SchedulingManager.GetManager(); | |
| } | |
| public void ExecuteTask(string typeName, string camsTaskKey = "") | |
| { | |
| try | |
| { | |
| var assembly = Assembly.Load(AppConstant.AssemblyName); | |
| var scheduleType = assembly.GetType(string.Format("{0}.{1}", AppConstant.CustomScheduleTaskNamespace, typeName)); | |
| var scheduleMethod = scheduleType.GetMethod(AppConstant.CustomScheduleTaskScheduleTaskname, BindingFlags.Public | BindingFlags.Static); | |
| if (camsTaskKey.IsNullOrEmpty()) | |
| { | |
| scheduleMethod.Invoke(null, new object[] { true, false }); | |
| } | |
| else | |
| { | |
| scheduleMethod.Invoke(null, new object[] { true, false, camsTaskKey }); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| throw ex; | |
| } | |
| } | |
| public ScheduledTaskData GetTaskByKey(string key) | |
| { | |
| return _manager.GetTaskData().Where(x => x.Key.ToLower() == key.ToLower() && !x.IsRunning).FirstOrDefault(); | |
| } | |
| public ScheduledTaskData GetTaskByTypeName(string typeName) | |
| { | |
| var scheduleType = string.Format("{0}.{1}", AppConstant.CustomScheduleTaskNamespace, typeName); | |
| return _manager.GetTaskData().Where(x => x.TaskName.ToLower() == scheduleType.ToLower() && !x.IsRunning).FirstOrDefault(); | |
| } | |
| public IQueryable<ScheduledTaskData> GetAllTasks() | |
| { | |
| return _manager.GetTaskData().Where(x => !x.IsRunning).OrderBy(x => x.Key); | |
| } | |
| public IQueryable<ScheduledTaskData> GetAllCustomTasks(IEnumerable<string> keys) | |
| { | |
| return _manager.GetTaskData().Where(x => keys.Contains(x.Key.ToLower())).OrderBy(x => x.Key); | |
| } | |
| public void StopTask(string key, bool force = false) | |
| { | |
| var schedulingManager = SchedulingManager.GetManager(); | |
| var existingTask = _manager.GetTaskData().FirstOrDefault(x => x.Key == key); | |
| if (existingTask != null) | |
| { | |
| if (!existingTask.IsRunning || (existingTask.IsRunning && force)) | |
| { | |
| _manager.DeleteTaskData(existingTask); | |
| _manager.SaveChanges(); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using System.Linq; | |
| using Telerik.Sitefinity.Scheduling.Model; | |
| namespace SitefinityWebApp.Tasks | |
| { | |
| public interface IGenericTask | |
| { | |
| void ExecuteTask(string typeName, string camsTaskKey = ""); | |
| void StopTask(string key, bool force); | |
| IQueryable<ScheduledTaskData> GetAllTasks(); | |
| IQueryable<ScheduledTaskData> GetAllCustomTasks(IEnumerable<string> keys); | |
| ScheduledTaskData GetTaskByKey(string key); | |
| ScheduledTaskData GetTaskByTypeName(string typeName); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using SitefinityWebApp.Helpers; | |
| using SitefinityWebApp.Utilities; | |
| using System; | |
| using System.Linq; | |
| using Telerik.Sitefinity.Configuration; | |
| using Telerik.Sitefinity.Scheduling; | |
| namespace SitefinityWebApp.Tasks | |
| { | |
| public class HousekeepingTask : ScheduledTask | |
| { | |
| public HousekeepingTask() | |
| { | |
| this.Key = ScheduleConstant.SampleTaskKey; | |
| } | |
| public override string TaskName { get { return this.GetType().FullName; } } | |
| public override void ExecuteTask() | |
| { | |
| try | |
| { | |
| // Logic to Execute Task | |
| ScheduleTask(rescheduleNewTask: true); | |
| } | |
| catch (Exception ex) | |
| { | |
| Logs.WriteError(ex); | |
| } | |
| } | |
| public static void ScheduleTask(bool init = false, bool rescheduleNewTask = false) | |
| { | |
| try | |
| { | |
| var executeTime = DateTime.UtcNow; | |
| var schedulingManager = SchedulingManager.GetManager(); | |
| var existingTask = schedulingManager.GetTaskData().FirstOrDefault(x => x.Key == ScheduleConstant.SampleTaskKey); | |
| if (existingTask == null || rescheduleNewTask) | |
| { | |
| var newTask = new HousekeepingTask | |
| { | |
| Key = ScheduleConstant.SampleTaskKey, | |
| ExecuteTime = executeTime | |
| }; | |
| schedulingManager.AddTask(newTask); | |
| } | |
| else | |
| { | |
| if (init) return; | |
| existingTask.ExecuteTime = executeTime; | |
| } | |
| SchedulingManager.RescheduleNextRun(); | |
| schedulingManager.SaveChanges(); | |
| } | |
| catch (Exception ex) | |
| { | |
| throw ex; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace SitefinityWebApp.Helpers | |
| { | |
| public static class AppConstant | |
| { | |
| public const string AssemblyName = "SitefinityWebApp"; | |
| public const string CustomScheduleTaskNamespace = "SitefinityWebApp.Tasks"; | |
| public const string CustomScheduleTaskScheduleTaskname = "ScheduleTask"; | |
| } | |
| public static class ScheduleConstant | |
| { | |
| public const string SampleTaskKey = "SampleTask-1234567"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment