Update Automatically Status with the help schedule()

Step 1:- Add a function in controller

In my case GuestPostAdminController:-

 public function updateStatusEvery24Hours()
    {
        // Get all records from the Guest_post table
        $guestPosts = Guest_post::all();
        Log::info('Checking status for URLs: ' . json_encode($guestPosts));

        foreach ($guestPosts as $post) {
            // Check if 24 hours have passed since the last update
            $lastUpdated = Carbon::parse($post->updated_at);
            $currentTime = Carbon::now();

            // if ($currentTime->diffInMinutes($lastUpdated) >= 1) {
            // if ($currentTime->diffInHours($lastUpdated) >= 24) {
            //     // Perform the URL check (similar to your existing logic)
            //     $isUrlWorking = $this->checkIfUrlIsWorking($post->url);

            //     // Update the status in the database
            //     $post->update(['status' => $isUrlWorking]);

            //     // Log the URL and its status
            //     Log::info('URL: ' . $post->url . ', Status: ' . ($isUrlWorking ? 'Active' : 'Inactive'));
            // }

            if ($currentTime->diffInMinutes($lastUpdated) >= 1) {
                // Perform the URL check (similar to your existing logic)
                $isUrlWorking = $this->checkIfUrlIsWorking($post->url);
    
                // Update the status in the database
                $post->update(['status' => $isUrlWorking]);
    
                // Log the URL and its status
                Log::info('URL: ' . $post->url . ', Status: ' . ($isUrlWorking ? 'Active' : 'Inactive'));
            }
        }

        Log::info('Status update completed.');
    }

    protected function checkIfUrlIsWorking($url)
    {
        try {
            // Send a simple GET request and check if it's successful
            $response = Http::get($url);
            Log::info('Checking status for URL: ' . $url . ', Response: ' . $response->status());

            return $response->successful();
        } catch (\Exception $e) {
            Log::error('Error checking URL: ' . $url . ', Error: ' . $e->getMessage());
            return false; // Error occurred, website is not reachable
        }
    }

Step 2:- Create a command

php artisan make:command UpdateStatusEvery24Hours
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\Admin\GuestPostAdminController;

class UpdateStatusEvery24Hours extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'guestpost:update-status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update status for URLs every 24 hours';

    /**
     * Execute the console command.
     *
     * @return int
     */
   

    public function handle()
    {
        // Create an instance of your controller
        $controller = new GuestPostAdminController;

        // Call the controller function
        $controller->updateStatusEvery24Hours();

        $this->info('Status update completed.');
    }
}

Step 3:- In your project

app\Console\Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('guestpost:update-status')->daily();
        $schedule->command('guestpost:update-status')->everyMinute();
        
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

In local project, you can run commands after minute

php artisan schedule:run

But when you live this code then you can put the command

Open crontab -e in your linux server

/opt/lampp

/opt/lampp# crontab -e

command:-

* * * * * cd /opt/lampp/htdocs/wizbrand/wz-account-admin-ms && php artisan schedule:run >> /dev/null 2>&1

Output:-

Automatically update status if url is Active or Inactive

Hopefully, It will help you…!!!!

Related Posts

Professional Skill Alignment Around MLOps Foundation Certification in Modern Workplaces

Introduction The MLOps Foundation Certification has emerged as a critical benchmark for professionals looking to bridge the gap between data science and production engineering. This guide is…

Read More

Certified AIOps Manager: Strategic Framework for Intelligent IT Operations

Introduction The Certified AIOps Manager program is a specialized training designed to help professionals lead the next wave of IT operations. This guide is for engineers and…

Read More

Advanced AIOps Architect Certification Roadmap for DevOps Engineers

Introduction The Certified AIOps Architect is a comprehensive professional program designed for engineers and architects who want to master the intersection of Artificial Intelligence and IT Operations….

Read More

Advanced Certified AIOps Professional Guide for Mastering AI Driven Operations Skills

Introduction Artificial Intelligence for IT Operations is the future of managing complex systems and large scale digital environments. The Certified AIOps Professional program is designed for those…

Read More

Certified AIOps Engineer Training to Boost Automation Monitoring and Career Growth

The Certified AIOps Engineer is a specialized professional program designed to integrate artificial intelligence into modern IT operations. As systems scale and generate massive amounts of telemetry…

Read More

Advanced Guide to AIOps Foundation Certification for Scalable IT Infrastructure

In an era where infrastructure and applications generate massive amounts of telemetry data, manual intervention is no longer a sustainable strategy for maintaining system uptime. The AIOps…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x