This commit is contained in:
Bozhidar 2024-04-24 22:19:31 +03:00
parent 60a57b6606
commit d791dd3c8a
3 changed files with 31 additions and 64 deletions

View file

@ -26,10 +26,23 @@ class Backup extends Model
static::creating(function ($model) { static::creating(function ($model) {
$model->status = 'pending'; $model->status = 'pending';
$model->checkCronJob();
}); });
} }
private function checkCronJob()
{
$findCronJob = CronJob::where('command', 'phyre-php artisan phyre:run-backup')->first();
if (! $findCronJob) {
$cronJob = new CronJob();
$cronJob->schedule = '*/5 * * * *';
$cronJob->command = 'phyre-php artisan phyre:run-backup';
$cronJob->user = 'root';
$cronJob->save();
}
}
protected function backupRelated() : Attribute protected function backupRelated() : Attribute
{ {
$relatedWith = $this->backup_type; $relatedWith = $this->backup_type;

View file

@ -2,87 +2,45 @@
namespace App\Models; namespace App\Models;
use App\ShellApi;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;
class CronJob extends Model class CronJob extends Model
{ {
use Sushi;
protected $fillable = [ protected $fillable = [
'schedule', 'schedule',
'command', 'command',
'user', 'user',
]; ];
protected $schema = [
'schedule' => 'string',
'command' => 'string',
'user' => 'string',
];
public static function boot() public static function boot()
{ {
parent::boot(); parent::boot();
static::creating(function ($model) { static::creating(function ($model) {
$addCron = ShellApi::callBin('cron-job-add', [ // $addCron = ShellApi::callBin('cron-job-add', [
$model->user, // $model->user,
$model->schedule, // $model->schedule,
$model->command, // $model->command,
]); // ]);
if (empty($addCron)) { // if (empty($addCron)) {
return false; // return false;
} // }
}); });
static::deleting(function ($model) { static::deleting(function ($model) {
$deleteCron = ShellApi::callBin('cron-job-delete', [ // $deleteCron = ShellApi::callBin('cron-job-delete', [
$model->user, // $model->user,
$model->schedule, // $model->schedule,
$model->command, // $model->command,
]); // ]);
if (empty($deleteCron)) { // if (empty($deleteCron)) {
return false; // return false;
} // }
}); });
} }
protected function sushiShouldCache()
{
return true;
}
public function getRows()
{
$user = ShellApi::exec('whoami');
$cronList = ShellApi::callBin('cron-jobs-list', [
$user,
]);
$rows = [];
if (! empty($cronList)) {
$cronList = json_decode($cronList, true);
if (! empty($cronList)) {
foreach ($cronList as $cron) {
if (isset($cron['schedule'])) {
$rows[] = [
'schedule' => $cron['schedule'],
'command' => $cron['command'],
'user' => $user,
'time' => time(),
];
}
}
}
}
return $rows;
}
} }

View file

@ -15,12 +15,8 @@ return new class extends Migration
$table->id(); $table->id();
$table->string('hosting_account_id')->nullable(); $table->string('hosting_account_id')->nullable();
$table->string('command'); $table->string('command');
$table->string('frequency'); $table->string('schedule');
$table->string('minute'); $table->string('user');
$table->string('hour');
$table->string('day_of_month');
$table->string('month');
$table->string('day_of_week');
$table->timestamps(); $table->timestamps();
}); });
} }