From e021309211a385fc73c368318a01c243056065bb Mon Sep 17 00:00:00 2001 From: Derrick Heesbeen Date: Mon, 6 Nov 2017 22:35:08 +0100 Subject: [PATCH] [TASK] #8 added cleanup cron --- Controller/Adminhtml/Emailcatcher/Cleanup.php | 45 +++++++++++++++++ Cron/Clean.php | 50 +++++++++++++++++++ etc/crontab.xml | 8 +++ .../experius_emailcatcher_index.xml | 8 +++ 4 files changed, 111 insertions(+) create mode 100644 Controller/Adminhtml/Emailcatcher/Cleanup.php create mode 100644 Cron/Clean.php create mode 100644 etc/crontab.xml diff --git a/Controller/Adminhtml/Emailcatcher/Cleanup.php b/Controller/Adminhtml/Emailcatcher/Cleanup.php new file mode 100644 index 0000000..6ea9a9d --- /dev/null +++ b/Controller/Adminhtml/Emailcatcher/Cleanup.php @@ -0,0 +1,45 @@ +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('*/*/'); + } +} diff --git a/Cron/Clean.php b/Cron/Clean.php new file mode 100644 index 0000000..aefd731 --- /dev/null +++ b/Cron/Clean.php @@ -0,0 +1,50 @@ +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; + } + + +} \ No newline at end of file diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100644 index 0000000..767574d --- /dev/null +++ b/etc/crontab.xml @@ -0,0 +1,8 @@ + + + + + 0 3 * * * + + + \ No newline at end of file diff --git a/view/adminhtml/ui_component/experius_emailcatcher_index.xml b/view/adminhtml/ui_component/experius_emailcatcher_index.xml index ac8c071..80360a5 100644 --- a/view/adminhtml/ui_component/experius_emailcatcher_index.xml +++ b/view/adminhtml/ui_component/experius_emailcatcher_index.xml @@ -10,6 +10,14 @@ experius_emailcatcher_index.experius_emailcatcher_grid_data_source experius_emailcatcher_columns + + + cleanup + Run Cleanup + */*/cleanup + + +