Update Status when each order time past 20 minutes

I use Laravel Lumen framework and jenssegers/laravel-mongodb package , my query in project is :

    $time_5_min_ago = Carbon::now()->subMinute(5);
    $time_10_min_ago = Carbon::now()->subMinute(10);
    $time_15_min_ago = Carbon::now()->subMinute(15);
    $time_20_min_ago = Carbon::now()->subMinute(20);



    return Order::where(function ($query)  use ($maxLat_try_one,$minLat_try_one,$maxLon_try_one,$minLon_try_one,$time_5_min_ago,$time_10_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
            ->whereBetween('source_latitude', [$minLat_try_one,$maxLat_try_one])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_5_min_ago)
            ->where('created_at', '>=', $time_10_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
            ->whereBetween('source_latitude', [$minLat_try_two,$maxLat_try_two])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_10_min_ago)
            ->where('created_at', '>=', $time_15_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_three,$minLat_try_three,$maxLon_try_three,$minLon_try_three,$time_15_min_ago,$time_20_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_three, $maxLon_try_three])
            ->whereBetween('source_latitude', [$minLat_try_three,$maxLat_try_three])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_15_min_ago)
            ->where('created_at', '>=', $time_20_min_ago);
    })->get($fields);

I want to any order exist in top query and in last orWehere query , order created_at was < 20 minutes ago and status still was pending that order status updated to suspend

Answers


Use Laravel's Commands combined with schedule and you will get what you want.

Here's a detailed explanation:

Step 1: Create a command called "ChangePendingToSuspended"

Open a console then execute

php artisan make:console ChangePendingToSuspended
Step 2: Open up ChangePendingToSuspended.php

you can find it in your app/Console/Commands/ directory and tamper with it's parameters like adding a description

protected $description = 'Changes the Requests which has been in pending status for a period of time to suspended status.';

and a signature

protected $signature = 'requests:clear-pending';

Ok, before you ask "What's a signature?" A signature is a way of executing the command from the console for example now you can manually initiate the ChangePendingToSuspended command from artisan like

php artisan requests:clear-pending
Step 3: Defining our command

Now you put your code into the handle method in your case it might be something in the context of:

public function handle(){
    \DB::table('requests')
        ->where('created_at','<',\Carbon\Carbon::now()->addMinutes(-20))
        ->update(['status'=>'suspended']);
}

Just use whatever method that you prefer to change the status in that command.

Step 4: Adding the command to the Schedule

Open up Kernel.php found in app\Console\ directory you will be presented with an array called $commands, add our class to it

 protected $commands = [
        Commands\Inspire::class,
        Commands\ChangePendingToSuspended::class,
    ];

Now go to the schedule method and schedule your newly created command

protected function schedule(Schedule $schedule)
    {
...
        $schedule->command('requests:change-pending-to-investigate')->everyFiveMinutes();
...
    }

Ok what's happening here is that now, every five minutes, the scheduler will execute our command ChangePendingToSuspended each five minutes, but there's still 1 more step, we need to make the schedule tick by adding it's cron job to our system.

Step 5: add the schedule cron entry to your server

this differs among servers and releases, whether you are using windows, or linux or osx

but here's the cron entry

for linux:

* * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

for windows (using the task scheduler):

* * * * path/to/php path/to/artisan schedule:run 1>> NUL 2>&1

I think you should use Eloquen attribute instead of update database every minute.

public function getStatusAttribute() {
 if($this->created > ....) {
   $status = "pending";
 } else if(...) {
   ....
 }
 return $status;
}

Need Your Help

shell script (running via cron) to enter password when prompted

linux bash cron centos rar

I have a daily cron task which automatically unrars a rar file and processes it's contents, however, the contents are now password protected so i'm wondering if there's a reliable way to echo the

Make a single file using picture/video and audio files in android

android-intent android-service android-mediaplayer android-mediarecorder android-mediascanner

I am developing an android app where users will see some picture or videos on screen and put voice comments over this. On finishing, user we see a new video which will have pictures or videos with ...