Cleanup export files after some time
[MAILPOET-1894]
This commit is contained in:
@@ -27,6 +27,7 @@ class Daemon {
|
||||
$this->executeSendingServiceKeyCheckWorker();
|
||||
$this->executePremiumKeyCheckWorker();
|
||||
$this->executeBounceWorker();
|
||||
$this->executeExportFilesCleanupWorker();
|
||||
// TODO: execute WooCommerceSync worker
|
||||
} catch (\Exception $e) {
|
||||
CronHelper::saveDaemonLastError($e->getMessage());
|
||||
@@ -75,4 +76,9 @@ class Daemon {
|
||||
return $migration->process();
|
||||
}
|
||||
|
||||
function executeExportFilesCleanupWorker() {
|
||||
$worker = $this->workers_factory->createExportFilesCleanupWorker($this->timer);
|
||||
return $worker->process();
|
||||
}
|
||||
|
||||
}
|
||||
|
27
lib/Cron/Workers/ExportFilesCleanup.php
Normal file
27
lib/Cron/Workers/ExportFilesCleanup.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace MailPoet\Cron\Workers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Subscribers\ImportExport\Export\Export;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class ExportFilesCleanup extends SimpleWorker {
|
||||
const TASK_TYPE = 'export_files_cleanup';
|
||||
const DELETE_FILES_AFTER_X_DAYS = 1;
|
||||
|
||||
function processTaskStrategy(ScheduledTask $task) {
|
||||
$iterator = new \GlobIterator(Export::getExportPath() . '/' . Export::getFilePrefix() . '*.*');
|
||||
foreach ($iterator as $file) {
|
||||
$name = $file->getPathname();
|
||||
$created = $file->getMTime();
|
||||
$now = new Carbon();
|
||||
if (Carbon::createFromTimestamp($created)->lessThan($now->subDays(self::DELETE_FILES_AFTER_X_DAYS))) {
|
||||
unlink($name);
|
||||
};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -94,4 +94,9 @@ class WorkersFactory {
|
||||
return new WooCommerceSyncWorker($this->woocommerce_segment, $timer);
|
||||
}
|
||||
|
||||
/** @return ExportFilesCleanup */
|
||||
function createExportFilesCleanupWorker($timer) {
|
||||
return new ExportFilesCleanup($timer);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -45,11 +45,19 @@ class Export {
|
||||
$this->subscriber_fields,
|
||||
$this->subscriber_custom_fields
|
||||
);
|
||||
$this->export_path = Env::$temp_path;
|
||||
$this->export_path = self::getExportPath();
|
||||
$this->export_file = $this->getExportFile($this->export_format_option);
|
||||
$this->export_file_URL = $this->getExportFileURL($this->export_file);
|
||||
}
|
||||
|
||||
static function getFilePrefix() {
|
||||
return 'MailPoet_export_';
|
||||
}
|
||||
|
||||
static function getExportPath() {
|
||||
return Env::$temp_path;
|
||||
}
|
||||
|
||||
function process() {
|
||||
$this->default_subscribers_getter->reset();
|
||||
try {
|
||||
@@ -183,7 +191,7 @@ class Export {
|
||||
|
||||
function getExportFile($format) {
|
||||
return sprintf(
|
||||
$this->export_path . '/MailPoet_export_%s.%s',
|
||||
$this->export_path . '/' . self::getFilePrefix() . '%s.%s',
|
||||
Security::generateRandomString(15),
|
||||
$format
|
||||
);
|
||||
|
@@ -27,6 +27,7 @@ class DaemonTest extends \MailPoetTest {
|
||||
'executeSendingServiceKeyCheckWorker' => null,
|
||||
'executePremiumKeyCheckWorker' => null,
|
||||
'executeBounceWorker' => null,
|
||||
'executeExportFilesCleanupWorker' => null,
|
||||
), $this);
|
||||
$data = array(
|
||||
'token' => 123
|
||||
@@ -44,7 +45,8 @@ class DaemonTest extends \MailPoetTest {
|
||||
'executeStatsNotificationsWorker' => Expected::exactly(1),
|
||||
'executeSendingServiceKeyCheckWorker' => Expected::exactly(1),
|
||||
'executePremiumKeyCheckWorker' => Expected::exactly(1),
|
||||
'executeBounceWorker' => Expected::exactly(1)
|
||||
'executeBounceWorker' => Expected::exactly(1),
|
||||
'executeExportFilesCleanupWorker' => Expected::exactly(1),
|
||||
), $this);
|
||||
$data = array(
|
||||
'token' => 123
|
||||
|
23
tests/integration/Cron/Workers/ExportFilesCleanupTest.php
Normal file
23
tests/integration/Cron/Workers/ExportFilesCleanupTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Cron\Workers;
|
||||
|
||||
use MailPoet\Cron\Workers\ExportFilesCleanup;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
|
||||
class ExportFilesCleanupTest extends \MailPoetTest {
|
||||
|
||||
function testItWorks() {
|
||||
$wp_upload_dir = wp_upload_dir();
|
||||
$old_file_path = $wp_upload_dir['basedir'] . '/mailpoet/MailPoet_export_old_file.csv';
|
||||
$new_file_path = $wp_upload_dir['basedir'] . '/mailpoet/MailPoet_export_new_file.csv';
|
||||
touch($old_file_path, time() - (60 * 60 * 24 * 7));
|
||||
touch($new_file_path);
|
||||
|
||||
$cleanup = new ExportFilesCleanup();
|
||||
$cleanup->processTaskStrategy(ScheduledTask::createOrUpdate([]));
|
||||
|
||||
$this->assertFileExists($new_file_path);
|
||||
$this->assertFileNotExists($old_file_path);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user