# Author: M. Dutt (hello@mditech.net) # Date: 27/11/2023 # Purpose: Laravel command to update the application (git pull and composer install) using Laravel scheduler. # # Running The Scheduler # The schedule:run Artisan command will evaluate all of your scheduled tasks and determine if they need to run based on the server's current time. # Create a cron job in your server # * * * * * cd /path/to/project && php artisan schedule:run >> storage/logs/cron.log 2>&1 # # Directory structure # project-dir # app # Console # Commands # UpdateApp.php # Kernel.php # ========================================================= command('mditech:update-app') // Don't edit this line ->timezone('Asia/Kolkata') ->hourly() ->sendOutputTo('storage/logs/schedule.log') ->emailOutputTo('email@example.com') ->emailOutputOnFailure('email@example.com'); } /** * Register the commands for the application. */ protected function commands(): void { $this->load(__DIR__ . '/Commands'); require base_path('routes/console.php'); } } ========================================================= runPull()) { $this->error("An error occurred while executing 'git pull'. \nLogs:"); foreach ($this->pullLog as $logLine) { $this->info($logLine); } return; } if ($this->alreadyUpToDate) { $this->info("The application is already up-to-date"); return; } if (!$this->runComposer()) { $this->error("Error while updating composer files. \nLogs:"); foreach ($this->composerLog as $logLine) { $this->info($logLine); } return; } $this->info("Succesfully updated the application."); } private function runPull() { $process = new Process(['git', 'pull']); $this->info("Running 'git pull'"); $process->run(function ($type, $buffer) { $this->pullLog[] = $buffer; if ($buffer == "Already up to date.\n") { $this->alreadyUpToDate = TRUE; } }); return $process->isSuccessful(); } private function runComposer() { $process = new Process(['composer', 'install']); $this->info("Running 'composer install'"); $process->run(function($type, $buffer) { $this->composerLog[] = $buffer; }); return $process->isSuccessful(); } }