Cleanup export files after some time

[MAILPOET-1894]
This commit is contained in:
Pavel Dohnal
2019-04-01 15:00:50 +02:00
committed by M. Shull
parent 9500c1b5d5
commit 08a3a66f4c
7 changed files with 75 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ class Daemon {
$this->executeSendingServiceKeyCheckWorker(); $this->executeSendingServiceKeyCheckWorker();
$this->executePremiumKeyCheckWorker(); $this->executePremiumKeyCheckWorker();
$this->executeBounceWorker(); $this->executeBounceWorker();
$this->executeExportFilesCleanupWorker();
// TODO: execute WooCommerceSync worker // TODO: execute WooCommerceSync worker
} catch (\Exception $e) { } catch (\Exception $e) {
CronHelper::saveDaemonLastError($e->getMessage()); CronHelper::saveDaemonLastError($e->getMessage());
@@ -75,4 +76,9 @@ class Daemon {
return $migration->process(); return $migration->process();
} }
function executeExportFilesCleanupWorker() {
$worker = $this->workers_factory->createExportFilesCleanupWorker($this->timer);
return $worker->process();
}
} }

View File

@@ -132,7 +132,7 @@ class WordPress {
from %s from %s
where deleted_at is null where deleted_at is null
group by type, status, scheduled_in group by type, status, scheduled_in
", ",
date('Y-m-d H:i:s', $wp->currentTime('timestamp')), date('Y-m-d H:i:s', $wp->currentTime('timestamp')),
self::SCHEDULED_IN_THE_PAST, self::SCHEDULED_IN_THE_PAST,
self::SCHEDULED_IN_THE_FUTURE, self::SCHEDULED_IN_THE_FUTURE,

View 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;
}
}

View File

@@ -94,4 +94,9 @@ class WorkersFactory {
return new WooCommerceSyncWorker($this->woocommerce_segment, $timer); return new WooCommerceSyncWorker($this->woocommerce_segment, $timer);
} }
/** @return ExportFilesCleanup */
function createExportFilesCleanupWorker($timer) {
return new ExportFilesCleanup($timer);
}
} }

View File

@@ -45,11 +45,19 @@ class Export {
$this->subscriber_fields, $this->subscriber_fields,
$this->subscriber_custom_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 = $this->getExportFile($this->export_format_option);
$this->export_file_URL = $this->getExportFileURL($this->export_file); $this->export_file_URL = $this->getExportFileURL($this->export_file);
} }
static function getFilePrefix() {
return 'MailPoet_export_';
}
static function getExportPath() {
return Env::$temp_path;
}
function process() { function process() {
$this->default_subscribers_getter->reset(); $this->default_subscribers_getter->reset();
try { try {
@@ -183,7 +191,7 @@ class Export {
function getExportFile($format) { function getExportFile($format) {
return sprintf( return sprintf(
$this->export_path . '/MailPoet_export_%s.%s', $this->export_path . '/' . self::getFilePrefix() . '%s.%s',
Security::generateRandomString(15), Security::generateRandomString(15),
$format $format
); );

View File

@@ -27,6 +27,7 @@ class DaemonTest extends \MailPoetTest {
'executeSendingServiceKeyCheckWorker' => null, 'executeSendingServiceKeyCheckWorker' => null,
'executePremiumKeyCheckWorker' => null, 'executePremiumKeyCheckWorker' => null,
'executeBounceWorker' => null, 'executeBounceWorker' => null,
'executeExportFilesCleanupWorker' => null,
), $this); ), $this);
$data = array( $data = array(
'token' => 123 'token' => 123
@@ -44,7 +45,8 @@ class DaemonTest extends \MailPoetTest {
'executeStatsNotificationsWorker' => Expected::exactly(1), 'executeStatsNotificationsWorker' => Expected::exactly(1),
'executeSendingServiceKeyCheckWorker' => Expected::exactly(1), 'executeSendingServiceKeyCheckWorker' => Expected::exactly(1),
'executePremiumKeyCheckWorker' => Expected::exactly(1), 'executePremiumKeyCheckWorker' => Expected::exactly(1),
'executeBounceWorker' => Expected::exactly(1) 'executeBounceWorker' => Expected::exactly(1),
'executeExportFilesCleanupWorker' => Expected::exactly(1),
), $this); ), $this);
$data = array( $data = array(
'token' => 123 'token' => 123

View 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);
}
}