Skip to content

Commit

Permalink
Add cron jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 24, 2024
1 parent c07cd6d commit 134fa99
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions web/app/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,70 @@ public static function boot()
{
parent::boot();

static::created(function ($model) {
$model->configureCronJobs();
static::creating(function ($model) {
$allCronJobs = [];
$oldCronJobs = self::where('user', $model->user)->get();
foreach ($oldCronJobs as $oldCronJob) {
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
}
$allCronJobs[$model->user][] = $model->toArray();

$model->configureCronJobs($allCronJobs);
});

static::updated(function ($model) {
$model->configureCronJobs();
static::updating(function ($model) {
$allCronJobs = [];
$oldCronJobs = self::where('user', $model->user)->get();
foreach ($oldCronJobs as $oldCronJob) {
if ($oldCronJob->id == $model->id) {
$allCronJobs[$model->user][] = $model->toArray();
continue;
}
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
}
$model->configureCronJobs($allCronJobs);
});

static::deleted(function ($model) {
$model->configureCronJobs();
static::deleting(function ($model) {
$allCronJobs = [];
$oldCronJobs = self::where('user', $model->user)->get();
foreach ($oldCronJobs as $oldCronJob) {
if ($oldCronJob->id == $model->id) {
continue;
}
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
}

$model->configureCronJobs($allCronJobs);
});
}
public function configureCronJobs()
public function configureCronJobs($cronJobs)
{
$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;
$now = now();
$user = $this->user;
$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;

if (!empty($cronJobs)) {
foreach ($cronJobs as $user => $cronJobs) {
foreach ($cronJobs as $cronJob) {
$cronContent .= $cronJob['schedule'] . ' ' . $cronJob['command'] . PHP_EOL;
}

$cronContent .= PHP_EOL;
$cronFile = '/tmp/crontab-' . $user;
file_put_contents($cronFile, $cronContent);

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

}
}

$cronContent .= PHP_EOL;
$cronFile = '/tmp/crontab-' . $user;
file_put_contents($cronFile, $cronContent);

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

return false;
}

Expand Down

0 comments on commit 134fa99

Please sign in to comment.