From d791dd3c8a8da7515182d96cd4015407a003cea2 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Wed, 24 Apr 2024 22:19:31 +0300 Subject: [PATCH] update --- web/app/Models/Backup.php | 13 ++++ web/app/Models/CronJob.php | 74 ++++--------------- ...23_11_23_214641_create_cron_jobs_table.php | 8 +- 3 files changed, 31 insertions(+), 64 deletions(-) diff --git a/web/app/Models/Backup.php b/web/app/Models/Backup.php index a0b88c43..8029abc0 100644 --- a/web/app/Models/Backup.php +++ b/web/app/Models/Backup.php @@ -26,10 +26,23 @@ public static function boot() static::creating(function ($model) { $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 { $relatedWith = $this->backup_type; diff --git a/web/app/Models/CronJob.php b/web/app/Models/CronJob.php index cc0dccbd..0b960f83 100644 --- a/web/app/Models/CronJob.php +++ b/web/app/Models/CronJob.php @@ -2,87 +2,45 @@ namespace App\Models; -use App\ShellApi; use Illuminate\Database\Eloquent\Model; -use Sushi\Sushi; class CronJob extends Model { - use Sushi; - protected $fillable = [ 'schedule', 'command', 'user', ]; - protected $schema = [ - 'schedule' => 'string', - 'command' => 'string', - 'user' => 'string', - ]; - public static function boot() { parent::boot(); static::creating(function ($model) { - $addCron = ShellApi::callBin('cron-job-add', [ - $model->user, - $model->schedule, - $model->command, - ]); - if (empty($addCron)) { - return false; - } +// $addCron = ShellApi::callBin('cron-job-add', [ +// $model->user, +// $model->schedule, +// $model->command, +// ]); +// if (empty($addCron)) { +// return false; +// } }); static::deleting(function ($model) { - $deleteCron = ShellApi::callBin('cron-job-delete', [ - $model->user, - $model->schedule, - $model->command, - ]); - if (empty($deleteCron)) { - return false; - } +// $deleteCron = ShellApi::callBin('cron-job-delete', [ +// $model->user, +// $model->schedule, +// $model->command, +// ]); +// if (empty($deleteCron)) { +// 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; - } } diff --git a/web/database/migrations/2023_11_23_214641_create_cron_jobs_table.php b/web/database/migrations/2023_11_23_214641_create_cron_jobs_table.php index 506cfb70..4e6ef1b5 100644 --- a/web/database/migrations/2023_11_23_214641_create_cron_jobs_table.php +++ b/web/database/migrations/2023_11_23_214641_create_cron_jobs_table.php @@ -15,12 +15,8 @@ public function up(): void $table->id(); $table->string('hosting_account_id')->nullable(); $table->string('command'); - $table->string('frequency'); - $table->string('minute'); - $table->string('hour'); - $table->string('day_of_month'); - $table->string('month'); - $table->string('day_of_week'); + $table->string('schedule'); + $table->string('user'); $table->timestamps(); }); }