Skip to content

Commit

Permalink
Add cron for resaving posts
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysTsymbal committed Jul 13, 2022
1 parent 4dd48ca commit 14d5b02
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
32 changes: 19 additions & 13 deletions Cron/ReSaveExistingPosts.php
Expand Up @@ -10,22 +10,25 @@
use Magefan\Blog\Model\Config;
use Magento\Framework\Stdlib\DateTime\DateTime;

/**
* ReSave Posts that have PublishTime <= CurrentTime In Order To They Be Visible - Need If FPC Is Enabled
*/
class ReSaveExistingPosts
{
/**
* @var Config
*/
protected $config;
private $config;

/**
* @var PostCollectionFactory
*/
protected $postCollectionFactory;
private $postCollectionFactory;

/**
* @var DateTime
*/
protected $date;
private $date;

/**
* @param Config $config
Expand All @@ -36,26 +39,29 @@ public function __construct(
Config $config,
PostCollectionFactory $postCollectionFactory,
DateTime $date
)
{
) {
$this->config = $config;
$this->postCollectionFactory = $postCollectionFactory;
$this->date = $date;
}


/**
* @return void
*/
public function execute()
{
if (!$this->config->isEnabled()) {
return;
}

$postCollection = $this->postCollectionFactory->create()
->addFieldToFilter('publish_time', array('gteq' => $this->date->gmtDate('Y-m-d H:i:s', strtotime('-2 minutes'))))
->addFieldToFilter('publish_time', array('lteq' => $this->date->gmtDate()));

var_dump($this->date->gmtDate());
var_dump(count($postCollection));
->addActiveFilter()
->addFieldToFilter('publish_time', ['gteq' => $this->date->gmtDate('Y-m-d H:i:s', strtotime('-2 minutes'))])
->addFieldToFilter('publish_time', ['lteq' => $this->date->gmtDate()]);

foreach ($postCollection as $post) {
var_dump($post->getId());exit;
$post->setOrigData('is_active', 0);
$post->afterCommitCallback();
}
}
}
}
18 changes: 18 additions & 0 deletions etc/cron_groups.xml
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
<group id="magefan_default">
<schedule_generate_every>1</schedule_generate_every>
<schedule_ahead_for>4</schedule_ahead_for>
<schedule_lifetime>2</schedule_lifetime>
<history_cleanup_every>10</history_cleanup_every>
<history_success_lifetime>60</history_success_lifetime>
<history_failure_lifetime>600</history_failure_lifetime>
<use_separate_process>1</use_separate_process>
</group>
</config>
14 changes: 14 additions & 0 deletions etc/crontab.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="magefan_default">
<job instance="Magefan\Blog\Cron\ReSaveExistingPosts" method="execute" name="magefan_blog_cron_resave_existing_posts">
<schedule>*/1 * * * *</schedule>
</job>
</group>
</config>

0 comments on commit 14d5b02

Please sign in to comment.