Bozhidar Slaveykov 1 year ago
parent
commit
98b25b8e93
3 changed files with 58 additions and 2 deletions
  1. 17 0
      bin/add-cron-job.sh
  2. 10 1
      web/app/Filament/Resources/CronJobResource.php
  3. 31 1
      web/app/Models/CronJob.php

+ 17 - 0
bin/add-cron-job.sh

@@ -0,0 +1,17 @@
+username=$1
+schedule=$2
+command=$3
+
+# Create a temporary file to hold the existing user's crontab
+crontab -u $username -l > /tmp/temp_crontab
+
+# Add a new cron job to the temporary file
+echo "$schedule $command" >> /tmp/temp_crontab
+
+# Install the modified crontab from the temporary file
+crontab -u $username /tmp/temp_crontab
+
+# Remove the temporary file
+rm /tmp/temp_crontab
+
+echo "done!"

+ 10 - 1
web/app/Filament/Resources/CronJobResource.php

@@ -23,7 +23,16 @@ class CronJobResource extends Resource
     {
         return $form
             ->schema([
-                //
+                Forms\Components\TextInput::make('schedule')
+                    ->autofocus()
+                    ->required()
+                    ->label('Schedule'),
+                Forms\Components\TextInput::make('command')
+                    ->required()
+                    ->label('Command'),
+                Forms\Components\TextInput::make('user')
+                    ->required()
+                    ->label('User'),
             ]);
     }
 

+ 31 - 1
web/app/Models/CronJob.php

@@ -10,9 +10,38 @@ class CronJob extends Model
 {
     use Sushi;
 
-    public function getRows()
+    protected $fillable = [
+        'schedule',
+        'command',
+        'user',
+    ];
+
+    protected $schema = [
+        'schedule' => 'string',
+        'command' => 'string',
+        'user' => 'string',
+    ];
+
+    public static function boot()
     {
+        parent::boot();
+
+        static::creating(function ($model) {
+            $args = escapeshellarg($model->user) .' '. escapeshellarg($model->schedule) . ' ' . escapeshellarg($model->command);
+            $addCron = shell_exec('/usr/local/phyre/bin/add-cron-job.sh ' . $args);
+            if (empty($addCron)) {
+                return false;
+            }
+        });
+    }
 
+    protected function sushiShouldCache()
+    {
+        return true;
+    }
+
+    public function getRows()
+    {
         $user = shell_exec('whoami');
         $cronList = shell_exec('/usr/local/phyre/bin/list-cron-jobs.sh ' . $user);
 
@@ -26,6 +55,7 @@ class CronJob extends Model
                             'schedule' => $cron['schedule'],
                             'command' => $cron['command'],
                             'user' => $user,
+                            'time'=> time(),
                         ];
                     }
                 }