Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 24, 2024
1 parent 60a57b6 commit d791dd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 64 deletions.
13 changes: 13 additions & 0 deletions web/app/Models/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
74 changes: 16 additions & 58 deletions web/app/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

0 comments on commit d791dd3

Please sign in to comment.