Skip to content

Commit

Permalink
Update CronJob.php
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 24, 2024
1 parent d791dd3 commit c07cd6d
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions web/app/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class CronJob extends Model
{
Expand All @@ -16,31 +17,51 @@ public static function boot()
{
parent::boot();

static::creating(function ($model) {
static::created(function ($model) {
$model->configureCronJobs();
});

// $addCron = ShellApi::callBin('cron-job-add', [
// $model->user,
// $model->schedule,
// $model->command,
// ]);
// if (empty($addCron)) {
// return false;
// }
static::updated(function ($model) {
$model->configureCronJobs();
});

static::deleted(function ($model) {
$model->configureCronJobs();
});
}
public function configureCronJobs()
{
$getAll = self::all();
if ($getAll->count() > 0) {
$users = [];
foreach ($getAll as $cron) {
$users[$cron->user][] = $cron->toArray();
}
foreach ($users as $user => $cronJobs) {
$now = now();
$cronContent = <<<EOT
# PhyrePanel Cron Jobs
# User: $user
# Generated at: $now
# Do not edit this file manually, it is automaticly generated by PhyrePanel
EOT;
$cronContent .= PHP_EOL . PHP_EOL;

static::deleting(function ($model) {
foreach ($cronJobs as $cronJob) {
$cronContent .= $cronJob['schedule'] . ' ' . $cronJob['command'] . PHP_EOL;
}

// $deleteCron = ShellApi::callBin('cron-job-delete', [
// $model->user,
// $model->schedule,
// $model->command,
// ]);
// if (empty($deleteCron)) {
// return false;
// }
$cronContent .= PHP_EOL;
$cronFile = '/tmp/crontab-' . $user;
file_put_contents($cronFile, $cronContent);

});
$output = shell_exec('crontab -u ' . $user . ' ' . $cronFile);
unlink($cronFile);

}
}

return false;
}

}

0 comments on commit c07cd6d

Please sign in to comment.