Skip to content

GRIN-78: Added cron that will clean grin message queue table #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Cron/CleanQueueCronjob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Grin\Module\Cron;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\ResourceConnection\ConnectionAdapterInterface;
use Magento\MysqlMq\Model\QueueManagement;
use Symfony\Component\Console\Output\OutputInterface;

class CleanQueueCronjob
{
/**
* @var ResourceConnection
*/
protected $resourceConnection;

public function __construct(ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}

/**
* Cronjob Description
*
* @return void
*/
public function execute(): void
{
$connection = $this->resourceConnection->getConnection();
$timeThirtyDaysInThePast = date("Y-m-d H:i:s", strtotime("-30 days", time()));
$timeSixtyDaysInThePast = date("Y-m-d H:i:s", strtotime("-60 days", time()));

$statusComplete = QueueManagement::MESSAGE_STATUS_COMPLETE;
$statusError = QueueManagement::MESSAGE_STATUS_ERROR;

$queueTable = $connection->getTableName("queue");
$select = $connection->select()
->from("$queueTable", "id")
->where("name = ?", "grin_module_webhook");

$result = $connection->fetchRow($select);
$grinQueueId = $result["id"];

$connection->delete("queue_message_status",
"updated_at <= '$timeThirtyDaysInThePast' and status = $statusComplete and queue_id = $grinQueueId"
);
$connection->delete("queue_message_status",
"updated_at <= '$timeSixtyDaysInThePast' and status = $statusError and queue_id = $grinQueueId"
);
}
}
9 changes: 9 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="grin_module_clean_queue_cronjob" instance="Grin\Module\Cron\CleanQueueCronjob" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>