Optimizely/Episerver Tidsstyrte jobber

Scheduled Jobs in Optimizely CMS 12

In Optimizely CMS 12 the admin mode is given a real facelift. The new admin mode written in React has risen like a phoenix from the ashes of the old Web Forms admin mode.

The schedules jobs has gotten a facelift too, where the duration of the last executions are shown as a nice graph like this.

Screenshot from admin mode showing a graph of execution time.

That's not all that is new. In CMS 11, you had to consider that the job could behave differently when started manually compared to when it was scheduled.

In CMS 12 things behave a little different, as the job is never run as the current user.

If you need the job to run with special privileges, you can do that. A use case could be a job that deletes some files.

In CMS 11 you could do:

PrincipalInfo.CurrentPrincipal = new GenericPrincipal(
    new GenericIdentity("Some Dummy User"),
    new[] { "WebEditors" }
);

In CMS 12 PrincipalInfo.CurrentPrincipal is read-only, so you need to access the principal through the IPrincipalAccessor like this:

_principalAccessor.Principal = new GenericPrincipal(
    new GenericIdentity("Some Dummy User"), 
    new[] { "WebEditors" }
);

And included in a scheduled job, it could look like this:

[ScheduledPlugIn(DisplayName = "My Scheduled Job")]
public class MyJob : ScheduledJobBase
{
    private readonly IPrincipalAccessor _principalAccessor;

    public MyJob (IPrincipalAccessor principalAccessor)
    {
        _principalAccessor = principalAccessor;
    }

    public override string Execute()
    {
        _principalAccessor.Principal = new GenericPrincipal(
            new GenericIdentity("Some Dummy User"), 
            new[] { "WebEditors" }
        );

        // Do some suff that needs WebEditor privileges.
    }
}

 

That's it!