Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

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

Vote:
 

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!

#247311
Edited, Jan 20, 2021 8:00
Vote:
 

Hi Fuji,

I am not sure but it might be related to HttpContext so can you please try this-

https://world.episerver.com/blogs/Magnus-Rahl/Dates/2010/12/Run-a-scheduled-job-as-a-specific-role/

#247318
Jan 20, 2021 9:10
Vote:
 

Scheduled job runs as your current user if you trigger it manually and as an anonymous user if trigger it automatically. You can set the role it uses when running automatically using the link Ravindra  sent above. Very common problem.

#247322
Jan 20, 2021 10:02
Vote:
 

If you have trouble falling asleep - you can read this post (https://blog.tech-fellow.net/2020/12/07/episerver-scheduled-jobs-under-the-hood/) and see what's under the hood and why it has different context when ran manually vs scheduled.

#248117
Feb 04, 2021 0:44
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.