mirror of
https://github.com/PhyreApps/PhyrePanel.git
synced 2024-11-21 23:20:24 +00:00
Add cron jobs
This commit is contained in:
parent
c07cd6da45
commit
134fa9932b
1 changed files with 50 additions and 30 deletions
|
@ -17,50 +17,70 @@ class CronJob extends Model
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue