feat: Added ChargeServers command & updated laravel schedule command

This commit is contained in:
IceToast 2022-06-16 14:54:42 +02:00 committed by IceToast
parent 681928c3ad
commit 7e17bb62ea
No known key found for this signature in database
GPG key ID: 1464353E063A5B97
2 changed files with 133 additions and 1 deletions

View file

@ -0,0 +1,122 @@
<?php
namespace App\Console\Commands;
use App\Models\Server;
use App\Notifications\ServersSuspendedNotification;
use Illuminate\Console\Command;
class ChargeServers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'servers:charge';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Charge all users with severs that are due to be charged';
/**
* A list of users that have to be notified
* @var array
*/
protected $usersToNotify = [];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Server::whereNull('suspended')->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("<fg=yellow>{$server->name}</> from user: <fg=blue>{$user->name}</> has been <fg=red>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("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$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("<fg=yellow>Notified user:</> <fg=blue>{$user->name}</>");
$user->notify(new ServersSuspendedNotification());
}
}
#reset array
$this->usersToNotify = array();
return true;
}
}

View file

@ -8,6 +8,16 @@ use Illuminate\Support\Facades\Storage;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\ChargeCreditsCommand::class,
Commands\ChargeServers::class,
];
/**
* Define the application's command schedule.
*
@ -16,7 +26,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('credits:charge')->hourly();
$schedule->command('servers:charge')->everyMinute();
$schedule->command('cp:versioncheck:get')->daily();
$schedule->command('payments:open:clear')->daily();