From c4f2be2edf34f8db2a55bccca0ce93a58d30d95f Mon Sep 17 00:00:00 2001 From: wxa Date: Sun, 28 Jul 2019 16:43:48 +0300 Subject: [PATCH] Clear the in_progress flag if WC sync task fails [MAILPOET-2202] --- lib/Cron/Workers/WooCommerceSync.php | 8 +++++++- .../Cron/Workers/WooCommerceSyncTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Cron/Workers/WooCommerceSync.php b/lib/Cron/Workers/WooCommerceSync.php index 5bd662a7e2..210c56a8bb 100644 --- a/lib/Cron/Workers/WooCommerceSync.php +++ b/lib/Cron/Workers/WooCommerceSync.php @@ -49,7 +49,13 @@ class WooCommerceSync extends SimpleWorker { $task->meta = ['in_progress' => true]; $task->save(); - $this->woocommerce_segment->synchronizeCustomers(); + try { + $this->woocommerce_segment->synchronizeCustomers(); + } catch (\Exception $e) { + $task->meta = null; + $task->save(); + throw $e; + } return true; } diff --git a/tests/integration/Cron/Workers/WooCommerceSyncTest.php b/tests/integration/Cron/Workers/WooCommerceSyncTest.php index bc69221011..2272153459 100644 --- a/tests/integration/Cron/Workers/WooCommerceSyncTest.php +++ b/tests/integration/Cron/Workers/WooCommerceSyncTest.php @@ -42,6 +42,20 @@ class WooCommerceSyncTest extends \MailPoetTest { expect($task->getMeta())->notEmpty(); } + function testItWillResetTheInProgressFlagOnFail() { + $task = $this->createScheduledTask(); + $this->woocommerce_segment->expects($this->once()) + ->method('synchronizeCustomers') + ->willThrowException(new \Exception('test error')); + try { + $this->worker->processTaskStrategy($task); + $this->fail('An exception should be thrown'); + } catch (\Exception $e) { + expect($e->getMessage())->equals('test error'); + expect($task->getMeta())->isEmpty(); + } + } + function testItWillRescheduleTaskIfItIsRunningForTooLong() { $this->woocommerce_segment->expects($this->once()) ->method('synchronizeCustomers');