From fb23f43f884283d66c401f2bb8d525475fd765e3 Mon Sep 17 00:00:00 2001 From: IceToast Date: Thu, 16 Jun 2022 14:54:42 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Added=20ChargeServers=20com?= =?UTF-8?q?mand=20&=20updated=20laravel=20schedule=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ChargeServers.php | 122 +++++++++++++++++++++++++ app/Console/Kernel.php | 5 +- 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/ChargeServers.php diff --git a/app/Console/Commands/ChargeServers.php b/app/Console/Commands/ChargeServers.php new file mode 100644 index 00000000..678ab4b7 --- /dev/null +++ b/app/Console/Commands/ChargeServers.php @@ -0,0 +1,122 @@ +with('users', 'products')->chunk(10, function ($servers) { + /** @var Server $server */ + foreach ($servers as $server) { + /** @var Product $product */ + $product = $server->product; + /** @var User $user */ + $user = $server->user; + + $billing_period = $product->billing_period; + + // check if server is due to be charged by comparing its last_billed date with the current date and the billing period + $newBillingDate = null; + switch($billing_period) { + case 'monthly': + $newBillingDate = $server->last_billed->addMonth(); + break; + case 'weekly': + $newBillingDate = $server->last_billed->addYear(); + break; + case 'daily': + $newBillingDate = $server->last_billed->addDay(); + break; + default: + $newBillingDate = $server->last_billed->addHour(); + break; + }; + if (!($newBillingDate <= now())) return; + + // check if user has enough credits to charge the server + if ($user->credits < $product->price) { + try { + #suspend server + $this->line("{$server->name} from user: {$user->name} has been suspended!"); + $server->suspend(); + + #add user to notify list + if (!in_array($user, $this->usersToNotify)) { + array_push($this->usersToNotify, $user); + } + } catch (\Exception $exception) { + $this->error($exception->getMessage()); + } + return; + } + + // charge credits to user + $this->line("{$user->name} Current credits: {$user->credits} Credits to be removed: {$product->price}"); + $user->decrement('credits', $product->price); + + // update server last_billed date + $server->last_billed = $newBillingDate; + } + + return $this->notifyUsers(); + }); + } + + /** + * @return bool + */ + public function notifyUsers() + { + if (!empty($this->usersToNotify)) { + /** @var User $user */ + foreach ($this->usersToNotify as $user) { + $this->line("Notified user: {$user->name}"); + $user->notify(new ServersSuspendedNotification()); + } + } + + #reset array + $this->usersToNotify = array(); + return true; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index fe25d44c..9a56bf90 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,7 +14,8 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - // + Commands\ChargeCreditsCommand::class, + Commands\ChargeServers::class, ]; /** @@ -25,7 +26,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - $schedule->command('credits:charge')->hourly(); + $schedule->command('servers:charge')->everyMinute(); //log cronjob activity $schedule->call(function () {