Files
FOSSBilling/src/modules/Cron/Service.php
T
Yağızhan Burak YakarandAdam Daley 19120d5a19 Introducing widgets (#2853)
* Add "widgets" to the list of core modules

* Patch 44 for widget support

* +Widgets module

* Basic widget discovery

* +Render logic

* +render_widget_slot Twig function

* +get_list

* +Basic settings page to list active widgets

* +Add an `enabled` column to toggle the widget

* +disconnectUnavailable

* Catch exceptions while rendering templates and display an error block

* Types, checks and stuff

* +Add widgets/batch_connect to the crons

Also removed the 11+ year old "finish fixing, time to sleep" docblock

* Demonstration: Replace hardcoded branding text with a widget

* Changed the directory structure

* +Add an error block for the admin panel

* +Added the `widgets` table

* Actually this is pretty unnecessary.

* "render_widget_slot" -> "render_widgets"

* Updated the dates

* Also inject admin/client API if accessible

* +Add an options column

* Implemented optional "add_br" toggle

* Log the exception

* Redid the widgets module

* Added a few new slots

* If even the error template fails, there are bigger problems. Just display the error instead of catching it.

* Created and implemented WidgetProviderInterface

---------

Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
2026-02-05 04:16:33 +00:00

119 lines
3.5 KiB
PHP

<?php
/**
* Copyright 2022-2025 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
namespace Box\Mod\Cron;
use FOSSBilling\Config;
use FOSSBilling\Environment;
use Symfony\Component\Filesystem\Path;
class Service
{
protected ?\Pimple\Container $di = null;
public function setDi(\Pimple\Container $di): void
{
$this->di = $di;
}
public function getDi(): ?\Pimple\Container
{
return $this->di;
}
public function getCronInfo(): array
{
$service = $this->di['mod_service']('system');
return [
'cron_path' => Path::join(PATH_ROOT, 'cron.php'),
'last_cron_exec' => $service->getParamValue('last_cron_exec'),
];
}
public function runCrons(): bool
{
$api = $this->di['api_system'];
$this->di['logger']->setChannel('cron')->info('Started executing cron jobs.');
// @core tasks
$this->_exec($api, 'hook_batch_connect');
$this->di['events_manager']->fire(['event' => 'onBeforeAdminCronRun']);
$this->_exec($api, 'invoice_batch_pay_with_credits');
$this->_exec($api, 'invoice_batch_activate_paid');
$this->_exec($api, 'invoice_batch_send_reminders');
$this->_exec($api, 'invoice_batch_generate');
$this->_exec($api, 'invoice_batch_invoke_due_event');
$this->_exec($api, 'order_batch_suspend_expired');
$this->_exec($api, 'order_batch_cancel_suspended');
$this->_exec($api, 'support_batch_ticket_auto_close');
$this->_exec($api, 'support_batch_public_ticket_auto_close');
$this->_exec($api, 'client_batch_expire_password_reminders');
$this->_exec($api, 'cart_batch_expire');
$this->_exec($api, 'email_batch_sendmail');
// Update the last time cron was executed
$this->di['mod_service']('system')->setParamValue('last_cron_exec', date('Y-m-d H:i:s'), true);
// Purge old sessions from the DB
$count = $this->clearOldSessions() ?? 0;
$this->di['logger']->setChannel('cron')->info("Cleared {$count} outdated sessions from the database.");
$this->di['events_manager']->fire(['event' => 'onAfterAdminCronRun']);
$this->di['logger']->setChannel('cron')->info('Finished executing cron jobs.');
return true;
}
/**
* @param string $method
*/
protected function _exec($api, $method, $params = null): void
{
try {
$api->{$method}($params);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
} finally {
if (Environment::isCLI()) {
echo "\e[32mSuccessfully ran {$method}({$params}).\e[0m\n";
}
}
}
/**
* @return string|null
*/
public function getLastExecutionTime()
{
$service = $this->di['mod_service']('system');
return $service->getParamValue('last_cron_exec');
}
public function isLate(): bool
{
$t1 = new \DateTime($this->getLastExecutionTime());
$t2 = new \DateTime('-6min');
return $t1 < $t2;
}
private function clearOldSessions(): ?int
{
$maxAge = time() - Config::getProperty('security.session_lifespan', 7200);
$sql = 'DELETE FROM session WHERE created_at <= :age';
return $this->di['db']->exec($sql, [':age' => $maxAge]);
}
}