Console icon indicating copy to clipboard operation
Console copied to clipboard

Publish seems to complete prior to item save via Scheduled Powershell Script

Open chummerz opened this issue 3 years ago • 8 comments

Expected Behavior

Scheduled task executes the powershell script sequentially when run as a scheduled job. IE Powershell script has a publish command after an item save.

Actual Behavior

Running the scheduled task manually works as expected. However when the script is triggered via scheduled job, the item save appears to occur after the publish. Powershell script run via ISE also works as expected.

Am using the "Never publish" and "hide version" as a flag concerning publish.

Steps to Reproduce the Problem

In an XP Scaled environment Write a powershell script which updates an item and publishes. ie disable "never publish" flag

Conversely have another script which updates and item as to unpublish it. enable "never publish" flag

Create a scheduled powershell script task via sitecore > system > tasks > schedules

Schedule the script

Await time of script give 10 to 15 minutes after target time

Script is executed however doesn't publish

Review item properties and never publish flag is unchecked as expected

Run the scheduled task manually, item is published as expected.

Sitecore 10.1 Sitecore PSE 6.2.0.34182 Windows Powershell 5.1

  • [ ] Tested issue with clean install of Sitecore and the latest available version of SPE.

  • [ ] Asked questions on the Sitecore Slack Chat channel.

  • [X] Reviewed questions and answers on the Sitecore Stack Exchange

github-logs.txt .

chummerz avatar Mar 18 '22 08:03 chummerz

Do you have an example of the script you are running? Can it be trimmed down to the minimal amount that repeats the issue?

michaellwest avatar Mar 18 '22 13:03 michaellwest

20220322-github-issue-1260-snippet.txt

Hi Michael, here is the snippet of the powershell script. Trimmed it down to the issue and managed to reproduce. Tried adding a pause but it doesn't appear to help.

chummerz avatar Mar 21 '22 23:03 chummerz

Hi Michael, upon inspecting, it appears that the scheduled powershell script task always skips publishing even when attempting to include -RepublishAll switch.

Is it possibly permissions related?

The script itself works when run via Powershell ISE and when running the associated task via the context menu via content editor. Unfortunately it always skips the publish when running via the scheduled task in an xp scaled environment. However runs as expected in xp single.

The data is saved correctly, and subsequent to a scheduled task being run, we run the publish step on the items manually.

Now looking to create a custom endpoint to be invoked as to force the publish, but finding this behaviour odd. Can't really think of any possibilities as to what's causing the publish to skip.

chummerz avatar Apr 27 '22 14:04 chummerz

Do you have a sample script of what you are executing? I wonder if the context is different. When running from the ISE a default context is set to the home item. From my schedule task, you need to be more specific about the path.

michaellwest avatar Apr 28 '22 13:04 michaellwest

Hi Michael,

Thanks for looking. I attached a sample in the previous post on March 22.

20220322-github-issue-1260-snippet.txt

Let me know if you can't retrieve it and I'll try attaching it again.

chummerz avatar Apr 28 '22 21:04 chummerz

I've also tried triggering the same script via Web API and forcing a path context similar to the default in ISE of "master:\content" however am getting the following error, even though the item does exist on master db

Get-Item : Cannot find path 'master' and id {DE3D738B-3790-3B83-BC8C-0FA3FDE56B39} because it does not exist.

Is there something else that needs to be set or does there need to be a flush? as this occurs on retrieval of second item in the script. The first was retrieved without error.

chummerz avatar Apr 29 '22 00:04 chummerz

Serious case of deja vu. Sorry it has taken so long to dig into this issue.

How are you determing the $db? I believe when this runs as a scheduled task it's using the core db as the shell website. You maybe need to do some investigating there.

I don't think the calls to sleep are necessary; the publish is synchronous. This is calling the publishmanager.

https://github.com/SitecorePowerShell/Console/blob/d4ca23455f4fd380cf7185ca27b054ef1ebbdfcd/src/Spe/Commands/Data/PublishItemCommand.cs#L138

michaellwest avatar Apr 29 '22 03:04 michaellwest

Thanks for pointing out the code location Michael, appreciate your time.

Thanks for also confirming synchronous as was speculating that somehow the script was running asynchronously.

In the meantime we had set up Web API along with Windows Task Scheduler and that is working as a workaround for now. Will revisit what appears to be a context / permission issue as next step.

chummerz avatar May 10 '22 07:05 chummerz