From f8d1e90d78e1abd45bf58c054e790dc6d7da456c Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Tue, 7 Nov 2023 17:45:46 -0800 Subject: [PATCH] Drop the Queue module (#1777) * Remove the Queue module and update massmailer Queue module was only used by the massmailer module & it duplicated the functionality of the email service, resulting in added complexity for no real benefit. This commit removes the Queue module outright and points the massmailer to the email service instead, which already implements queing, retires, and time limits. * Added the ability to view the email queue * Minor front-end improvements, added removal patch * Cleanly handle a client being removed --- src/install/sql/content.sql | 2 +- src/install/sql/content_test.sql | 2 +- src/library/FOSSBilling/UpdatePatcher.php | 24 ++ src/modules/Email/Api/Admin.php | 23 ++ src/modules/Email/Service.php | 40 ++- .../html_admin/mod_email_settings.html.twig | 74 ++++- src/modules/Massmailer/Api/Admin.php | 33 +- src/modules/Massmailer/Service.php | 40 +-- .../html_admin/mod_massmailer_index.html.twig | 26 +- src/modules/Queue/Api/Admin.php | 299 ------------------ src/modules/Queue/Service.php | 136 -------- .../html_admin/mod_queue_settings.html.twig | 83 ----- src/modules/Queue/icon.svg | 1 - src/modules/Queue/manifest.json | 12 - .../bb-modules/mod_queue/Api_AdminTest.php | 77 ----- 15 files changed, 169 insertions(+), 703 deletions(-) delete mode 100644 src/modules/Queue/Api/Admin.php delete mode 100644 src/modules/Queue/Service.php delete mode 100644 src/modules/Queue/html_admin/mod_queue_settings.html.twig delete mode 100644 src/modules/Queue/icon.svg delete mode 100644 src/modules/Queue/manifest.json delete mode 100644 tests/integration/bb-modules/mod_queue/Api_AdminTest.php diff --git a/src/install/sql/content.sql b/src/install/sql/content.sql index 26e07a185..8c24d7b6e 100644 --- a/src/install/sql/content.sql +++ b/src/install/sql/content.sql @@ -315,7 +315,7 @@ LOCK TABLES `setting` WRITE; INSERT INTO `setting` (`id`, `param`, `value`, `public`, `category`, `hash`, `created_at`, `updated_at`) VALUES - (1,'last_patch','36',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), + (1,'last_patch','37',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (2,'company_name','Company Name',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (3,'company_email','company@email.com',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (4,'company_signature','FOSSBilling.org - Client Management, Invoice and Support Software',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), diff --git a/src/install/sql/content_test.sql b/src/install/sql/content_test.sql index 462bdfdc9..a4b0e5763 100644 --- a/src/install/sql/content_test.sql +++ b/src/install/sql/content_test.sql @@ -480,7 +480,7 @@ LOCK TABLES `setting` WRITE; INSERT INTO `setting` (`id`, `param`, `value`, `public`, `category`, `hash`, `created_at`, `updated_at`) VALUES - (1,'last_patch','36',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), + (1,'last_patch','37',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (2,'company_name','FOSSBilling demo',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (3,'company_email','demo@fossbilling.org',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), (4,'company_signature','FOSSBilling.org - Client Management, Invoice and Support Software',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'), diff --git a/src/library/FOSSBilling/UpdatePatcher.php b/src/library/FOSSBilling/UpdatePatcher.php index 158046cc5..b57e833a3 100644 --- a/src/library/FOSSBilling/UpdatePatcher.php +++ b/src/library/FOSSBilling/UpdatePatcher.php @@ -334,6 +334,30 @@ class UpdatePatcher implements InjectionAwareInterface ]; $this->executeFileActions($fileActions); }, + 37 => function () { + // Patch to complete remove the outdated queue module. + // @see https://github.com/FOSSBilling/FOSSBilling/pull/1777 + + // Drop the old queue tables. + $q = "DROP TABLE queue;"; + $this->executeSql($q); + + $q = "DROP TABLE queue_message;"; + $this->executeSql($q); + + $ext_service = $this->di['mod_service']('extension'); + // If the queue extension exists, uninstall it. + $queue_ext = $ext_service->findExtension('mod', 'queue'); + if ($queue_ext instanceof \Model_Extension) { + $ext_service->uninstall($queue_ext); + } + + // Finally, remove old queue module from the disk. + $fileActions = [ + PATH_MODS . DIRECTORY_SEPARATOR . 'Queue' => 'unlink', + ]; + $this->executeFileActions($fileActions); + }, ]; ksort($patches, SORT_NATURAL); diff --git a/src/modules/Email/Api/Admin.php b/src/modules/Email/Api/Admin.php index 42ae53895..342bc23b3 100644 --- a/src/modules/Email/Api/Admin.php +++ b/src/modules/Email/Api/Admin.php @@ -406,4 +406,27 @@ class Admin extends \Api_Abstract return true; } + + public function get_queue(array $data) + { + $per_page = $data['per_page'] ?? $this->di['pager']->getPer_page(); + [$sql, $params] = $this->getService()->queueGetSearchQuery($data); + $pager = $this->di['pager']->getSimpleResultSet($sql, $params, $per_page); + + foreach ($pager['list'] as $key => $item) { + $pager['list'][$key] = [ + 'id' => $item['id'] ?? '', + 'recipient' => $item['recipient'] ?? '', + 'subject' => $item['subject'] ?? '', + 'content' => $item['content'] ?? '', + 'to_name' => $item['to_name'] ?? '', + 'status' => $item['status'] ?? '', + 'tries' => $item['tries'] ?? '', + 'created_at' => $item['created_at'] ?? '', + 'updated_at' => $item['updated_at'] ?? '', + ]; + } + + return $pager; + } } diff --git a/src/modules/Email/Service.php b/src/modules/Email/Service.php index 779c37e6a..1f75e3932 100644 --- a/src/modules/Email/Service.php +++ b/src/modules/Email/Service.php @@ -389,6 +389,35 @@ class Service implements \FOSSBilling\InjectionAwareInterface return [$query, $bindings]; } + public function queueGetSearchQuery($data) + { + $query = 'SELECT * FROM mod_email_queue'; + + $search = $data['search'] ?? null; + + $where = []; + $bindings = []; + + if ($search) { + $search = "%$search%"; + + $where[] = '(recipient LIKE :recipient OR subject LIKE :subject OR content LIKE :content OR to_name LIKE :to_name)'; + + $bindings[':recipient'] = $search; + $bindings[':subject'] = $search; + $bindings[':content'] = $search; + $bindings[':to_name'] = $search; + } + + if (!empty($where)) { + $query = $query . ' WHERE ' . implode(' AND ', $where); + } + + $query .= ' ORDER BY updated_at DESC'; + + return [$query, $bindings]; + } + public function templateToApiArray(\Model_EmailTemplate $model, $deep = false) { $data = [ @@ -609,17 +638,6 @@ class Service implements \FOSSBilling\InjectionAwareInterface $message = $e->getMessage(); error_log($message); - // Prevent mass retries of emails if one of them is "invalid" - if (str_contains($message, 'Invalid address:')) { - try { - $this->di['db']->trash($queue); - } catch (\Exception $e) { - error_log($e->getMessage()); - } - - return true; - } - if ($queue->priority) { --$queue->priority; } diff --git a/src/modules/Email/html_admin/mod_email_settings.html.twig b/src/modules/Email/html_admin/mod_email_settings.html.twig index 2aadcccac..b35646228 100644 --- a/src/modules/Email/html_admin/mod_email_settings.html.twig +++ b/src/modules/Email/html_admin/mod_email_settings.html.twig @@ -21,6 +21,7 @@ {% set active_menu = 'system' %} {% block content %} + {% set params = admin.extension_config_get({ 'ext': 'mod_email' }) %}
{% include 'partial_search.html.twig' %}
- +
@@ -92,7 +96,8 @@ {% if template.enabled == 0 %} {% endif %} - {{ mf.q(template.enabled) }} + {{ mf.q(template.enabled) }} +
{{ 'Subject'|trans }} @@ -175,7 +180,64 @@ - {% set params = admin.extension_config_get({ 'ext': 'mod_email' }) %} +
+ {% include 'partial_search.html.twig' %} +
+

{{ 'FOSSBilling queues emails to have them be sent via cron in the background. Here you may find the list of queued emails and how many times the system has tried to send them.'|trans }}

+

{{ 'Emails that have been sent or that failed to be sent will not be listed here.'|trans }}

+ + + + + + + + + + + + + + {% set queue = admin.email_get_queue({ 'per_page': 100, 'page': request.page }|merge(request)) %} + {% for i, queuedEmail in queue.list %} + + + + + + + + + + {% else %} + + + + {% endfor %} + +
{{ 'Subject'|trans }}{{ 'To'|trans }}{{ 'To (Email)'|trans }}{{ 'Status'|trans }}{{ 'Tries'|trans }}{{ 'Created at'|trans }}{{ 'Updated at'|trans }}
+ {{ queuedEmail.subject }} + + {{ queuedEmail.to_name }} + + {{ queuedEmail.recipient }} + + {% if queuedEmail.status == 'unsent' %} + + {% else %} + + {% endif %} + {{ queuedEmail.status }} + + {{ queuedEmail.tries }} / {{ params.cancel_after|default('5') }} + + {{ queuedEmail.created_at|format_date }} + + {{ queuedEmail.updated_at|format_date }} +
{{ 'The list is empty.'|trans }}
+
+
+
@@ -188,15 +250,15 @@
{{ 'Send emails per cron run (0 = unlimited)'|trans }} - +
{{ 'Max email sending time in minutes (0 = unlimited, default 5 min.)'|trans }} - +
{{ 'Cancel sending email after unsuccessful tries (0 = do not cancel)'|trans }} - +
diff --git a/src/modules/Massmailer/Api/Admin.php b/src/modules/Massmailer/Api/Admin.php index b2ef22298..ffba9696c 100644 --- a/src/modules/Massmailer/Api/Admin.php +++ b/src/modules/Massmailer/Api/Admin.php @@ -154,7 +154,7 @@ Order our services at {{ "order"|link }} throw new \FOSSBilling\InformationException('Add some content before sending message'); } - $this->getService()->sendMessage($model, $client_id); + $this->getService()->sendMessage($model, $client_id, true); $this->di['logger']->info('Sent test mail message #%s to client ', $model->id); @@ -175,22 +175,9 @@ Order our services at {{ "order"|link }} throw new \FOSSBilling\InformationException('Add some content before sending message'); } - $mod = $this->di['mod']('massmailer'); - $c = $mod->getConfig(); - $interval = isset($c['interval']) ? (int) $c['interval'] : 30; - $max = isset($c['limit']) ? (int) $c['limit'] : 25; - $clients = $this->getService()->getMessageReceivers($model, $data); foreach ($clients as $c) { - $d = [ - 'queue' => 'massmailer', - 'mod' => 'massmailer', - 'handler' => 'sendMail', - 'max' => $max, - 'interval' => $interval, - 'params' => ['msg_id' => $model->id, 'client_id' => $c['id']], - ]; - $this->di['api_admin']->queue_message_add($d); + $this->getService()->sendMail(['msg_id' => $model->id, 'client_id' => $c['id']]); } $model->status = 'sent'; @@ -274,6 +261,22 @@ Order our services at {{ "order"|link }} ]; } + /** + * Returns the email associated with the test client + * + * @return string + */ + public function get_test_client(): string + { + try { + $client = $this->di['mod_service']('client')->get(['id' => $this->_getTestClientId()]); + } catch (\Exception) { + return 'Unknown'; + } + + return $client->email; + } + private function _getTestClientId() { $mod = $this->di['mod']('massmailer'); diff --git a/src/modules/Massmailer/Service.php b/src/modules/Massmailer/Service.php index 8bc3bad61..68522c55d 100644 --- a/src/modules/Massmailer/Service.php +++ b/src/modules/Massmailer/Service.php @@ -138,7 +138,7 @@ class Service implements \FOSSBilling\InjectionAwareInterface return [$ps, $pc]; } - public function sendMessage($model, $client_id) + public function sendMessage($model, $client_id, $sendNow = false) { [$ps, $pc] = $this->getParsed($model, $client_id); @@ -169,32 +169,8 @@ class Service implements \FOSSBilling\InjectionAwareInterface return true; } - // Setup the email settings & recipients - $settings = $this->di['mod']('email')->getConfig(); - $transport = $settings['mailer'] ?? 'sendmail'; - $customDSN = $settings['custom_dsn'] ?? null; - $sender = [ - 'email' => $data['from'], - 'name' => $data['from_name'], - ]; - $recipient = [ - 'email' => $data['to'], - 'name' => $data['to_name'], - ]; - - try { - // Send the email - $mail = new \FOSSBilling\Mail($sender, $recipient, $data['subject'], $data['content'], $transport, $customDSN); - $mail->send($settings); - } catch (\Exception $e) { - error_log($e->getMessage()); - } - - // Log the email activity to the database if enabled - if (isset($settings['log_enabled']) && $settings['log_enabled']) { - $activityService = $this->di['mod_service']('activity'); - $activityService->logEmail($data['subject'], $client_id, $data['from'], $data['to'], $data['content']); - } + $emailService = $this->di['mod_service']('email'); + $emailService->sendMail($data['to'], $data['from'], $data['subject'], $data['content'], $data['to_name'], $data['from_name'], $data['client_id'], null, $sendNow); return true; } @@ -214,16 +190,6 @@ class Service implements \FOSSBilling\InjectionAwareInterface return $row; } - public static function onAfterAdminCronRun(\Box_Event $event) - { - try { - $di = $event->getDi(); - $di['api_admin']->queue_execute(['queue' => 'massmailer']); - } catch (\Exception $e) { - error_log('Error executing massmailer queue: ' . $e->getMessage()); - } - } - public function sendMail($params) { $model = $this->di['db']->load('mod_massmailer', $params['msg_id']); diff --git a/src/modules/Massmailer/html_admin/mod_massmailer_index.html.twig b/src/modules/Massmailer/html_admin/mod_massmailer_index.html.twig index a5bd4be2c..6839d540f 100644 --- a/src/modules/Massmailer/html_admin/mod_massmailer_index.html.twig +++ b/src/modules/Massmailer/html_admin/mod_massmailer_index.html.twig @@ -115,32 +115,9 @@ {% set params = admin.extension_config_get({"ext":"mod_massmailer"}) %} + data-api-reload="1"> -
-
-
-
- -
-
- -
-
- -
-
- -
-
- -
-
-
-
@@ -159,6 +136,7 @@ {% endif %}
+ {{ 'Test emails will be sent to: '|trans }} {{ admin.massmailer_get_test_client }}