diff --git a/web/app/Models/CronJob.php b/web/app/Models/CronJob.php index 0b960f83..eff27036 100644 --- a/web/app/Models/CronJob.php +++ b/web/app/Models/CronJob.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; class CronJob extends Model { @@ -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 = <<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; } }