Skip to content

Commit

Permalink
[TASK] #8 added cleanup cron
Browse files Browse the repository at this point in the history
  • Loading branch information
Derrick Heesbeen authored and Derrick Heesbeen committed Nov 6, 2017
1 parent 2c06889 commit e021309
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Controller/Adminhtml/Emailcatcher/Cleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* A Magento 2 module named Experius/EmailCatcher
* Copyright (C) 2016 Derrick Heesbeen
*
* This file included in Experius/EmailCatcher is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/

namespace Experius\EmailCatcher\Controller\Adminhtml\Emailcatcher;


class Cleanup extends \Magento\Backend\App\Action {

protected $clean;

public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Experius\EmailCatcher\Cron\Clean $clean
){
$this->clean = $clean;

parent::__construct($context);
}


public function execute(){

$resultRedirect = $this->resultRedirectFactory->create();

try {
$deleteCount = $this->clean->execute();
} catch (\Exception $e){
$this->messageManager->addError($e->getMessage());
return $resultRedirect->setPath('*/*/');
}

$this->messageManager->addSuccessMessage(__('Removed %1 records from %2 days ago or older',$deleteCount,'30'));

return $resultRedirect->setPath('*/*/');
}
}
50 changes: 50 additions & 0 deletions Cron/Clean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php


namespace Experius\EmailCatcher\Cron;

class Clean
{

protected $logger;

protected $resourceConnection;

/**
* Constructor
*
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\ResourceConnection $resourceConnection
){
$this->logger = $logger;
$this->resourceConnection = $resourceConnection;
$this->connection = $this->resourceConnection->getConnection();
}

/**
* Execute the cron
*
* @return void
*/
public function execute()
{

$numberOfDays = 30;

$where = "created_at < '" . date('c', time()-($numberOfDays*(3600*24))) ."'";

$deleteCount = $this->connection->delete(
$this->resourceConnection->getTableName('experius_emailcatcher'),
$where
);

$this->logger->addInfo(__('Experius EmailCatcher Cleanup: Removed %1 records',$deleteCount));

return $deleteCount;
}


}
8 changes: 8 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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 instance="Experius\EmailCatcher\Cron\Clean" method="execute" name="experius_emailcatcher_clean">
<schedule>0 3 * * *</schedule>
</job>
</group>
</config>
8 changes: 8 additions & 0 deletions view/adminhtml/ui_component/experius_emailcatcher_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<item name="deps" xsi:type="string">experius_emailcatcher_index.experius_emailcatcher_grid_data_source</item>
</item>
<item name="spinner" xsi:type="string">experius_emailcatcher_columns</item>
<item name="buttons" xsi:type="array">
<item name="add" xsi:type="array">
<item name="name" xsi:type="string">cleanup</item>
<item name="label" translate="true" xsi:type="string">Run Cleanup</item>
<item name="url" xsi:type="string">*/*/cleanup</item>
<!--<item name="on_click" xsi:type="string">deleteConfirm('Are you sure you want to do this?')</item>-->
</item>
</item>
</argument>
<dataSource name="experius_emailcatcher_grid_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
Expand Down

0 comments on commit e021309

Please sign in to comment.