I want to implement a function to run RebuildStaticCache Task every time a page is published, therefore I want to override the onAfterWrite method, but do not know how to run tasks within the method? Thank you.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
I have implemented the following and it seems working:
public function onAfterWrite()
{
$urls = array();
$urls[] = $this->Link();
$request = '';
$_GET['urls'] =$urls;
parent::onAfterWrite();
$staticPublisher = new RebuildStaticCacheTask();
$staticPublisher->run($request);
}
Just not sure this is the correct/best way to to do it, not sure what the $request variable does but can not be omitted.
Any better ideas will be appreicated.
I don't think you want to do that. I tend to use staticpublishqueue instead of staticpublisher so it's been a while, but I believe this module is set up to automatically republish the current page whenever it's published in the CMS. If you want to define some other pages that ALSO get published at the same time, you should define those in Page::subPagesToCache (see https://github.com/silverstripe-labs/silverstripe-staticpublisher/blob/master/docs/en/StaticPublisher.md). Your site will slow down VERY fast if you're republishing every page onAfterWrite.
I suggest you set up subPagesToCache properly and then have RebuildStaticCacheTask set up to run periodically just to catch any stragglers. That could mean every 24 hours or much more often depending on the site.