Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Scheduled job when triggered automatically ignores expired descendants, but not when triggered manually

$
0
0

Hello all! I have this scheduled job:

    [ScheduledPlugIn(DisplayName = "Archive Expired Descendants", Description = "")]
    public class ArchiveExiredDescendants : ScheduledJobBase
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly IContentRepository ContentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
        private bool _stopSignaled;
        public ArchiveExiredDescendants()
        {
            IsStoppable = true;
        }
        public override void Stop()
        {
            _stopSignaled = true;
        }
        public override string Execute()
        {
            try
            {
                ParentPage parentPage = ContentRepository.GetChildren<ParentPage>(Global.StartPage.ContentLink).FirstOrDefault();
                if(parentPage == null)
                {
                    return "Can't Find Parent Page.";
                }
                List<ChildPage> childPages = parentPage.GetDescendants<ChildPage>().ToList();
                if(!childPages.Any())
                {
                    return "No Child Pages Were Found.";
                }
                int i = 0;
                int ii = 0;
                foreach(ChildPage childPage in childPages)
                {
                    try
                    {
                        if(childPage == null) continue;
                        ii++;
                        if(childPage.CheckPublishedStatus(PagePublishedStatus.Published)) continue;
                        ContentRepository.Move(childPage.ContentLink, parentPage.ArchiveFolder);
                        i++;
                    }
                    catch(Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _stopSignaled ? "Stop of Job Was Called." : $"Job Completed. {i} ({ii}) Child Pages Were Archived.";
            }
            catch(Exception ex)
            {
                Logger.Error(ex.Message);
                return $"Job Failed. {ex.Message}";
            }
        }
    }

GetDescendants method:

    public static class PageDataExtentions
    {
        public static IEnumerable<T> GetDescendants<T>(this PageData page)
        {
            var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            var contentType = repository.Load(typeof(T));
            int? pageTypeID = contentType.ID;
            if (pageTypeID.HasValue)
            {
                PropertyCriteria pageTypeCriteria = new PropertyCriteria();
                pageTypeCriteria.Condition = CompareCondition.Equal;
                pageTypeCriteria.Name = "PageTypeID";
                pageTypeCriteria.Type = PropertyDataType.PageType;
                pageTypeCriteria.Value = pageTypeID.Value.ToString();
                PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
                criterias.Add(pageTypeCriteria);
                var pageCriteriaQueryService = ServiceLocator.Current
                                               .GetInstance<IPageCriteriaQueryService>();
                var descendants = pageCriteriaQueryService
                                    .FindPagesWithCriteria(page.ContentLink.ToPageReference(), criterias)
                                    .OrderByDescending(i => i.StartPublish ?? i.Created)
                                    .Cast<T>();
                return descendants;
            }
            return null;
        }
    }

Basically the purpose of the job is to find ParentPage. It has ArchiveFolder property. And to move every expired ChildPage of ParentPage into ArchiveFolder.

Job triggers by schedule successfully, but the issue is when it's triggered automatically, it ignores expired child pages, but when triggered manually - everything works as supposed to.

Any ideas why? Thank you very much!


Viewing all articles
Browse latest Browse all 8020

Trending Articles