May Sale! All accounts are 40% off this week.

GodziLaravel's avatar

How to change the max_execution_time in Laravel ?

Hello ,

Is it possible to change the value of max_execution_time outside of php.ini ?

I want it for only specific controller !

Thanks

0 likes
11 replies
tykus's avatar

You can ini_set('max_execution_time', $amountOfTime); in the controller.

I would ask if there was a way to avoid this approach by either making the work smaller/more efficient, or moving it to a queued job (if the user doesn't need to result immediately)

GodziLaravel's avatar

@tykus it's already queued !

I would like to use this because I have to update my table using a specific API .

I need to loop about 4000 elements and update my table . this task is repeated every hour ( crone job) .

tykus's avatar

If it is queued, why do you need to increase the execution time in the controller?

GodziLaravel's avatar

@tykus

Do you mean that if the task is queued it will not take in consideration 'max_execution_time?

This is the queued class bellow :

And as you can see this is the loop i'm talking about :foreach ($dealsTeamleader["data"] as $cKey => $cValue) {

$dealsTeamleader["data"] is more than 4000 element and for every element I have to call another method which is store() or update(). so it takes probably 15 min or more !

<?php


namespace App\CustomClass\Teamleader\Sync;


use App\Http\Traits\CreateDealFromApi;
use App\Http\Traits\TeamLeaderAPI;
use App\Http\Traits\UpdateDealFromApi;
use App\TeamleaderDeal;
use App\TeamleaderDealPhase;
use Carbon\Carbon;

class DealSync
{

    use TeamLeaderAPI;
    use UpdateDealFromApi;
    use CreateDealFromApi;
    private $dealSyncDate;
    private $alreadySync = false;
    private $teamleaderUpdateSinceHours;

    function __construct()
    {
        $timeNow = Carbon::now()->format('c');
        $this->dealSyncDate = \App\TeamleaderApi::first();

        if($this->dealSyncDate["deal_sync_date"] === null) {
            $this->dealSyncDate->update([
                "company_sync_date"=>Carbon::now()->format('c')
            ]);
        }


        $lastDealSyncDate = Carbon::parse($this->dealSyncDate["deal_sync_date"]);
        $this->teamleaderUpdateSinceHours = (int)(config('teamleader.teamleader_update_since_hours'));
        if ($lastDealSyncDate->diffInMinutes($timeNow) < (int)config('teamleader.time_between_every_update_in_minutes')) $this->alreadySync = true;
    }

    public function addOrUpdate($manual = false)
    {

        $filter = [
            'filter' => [
                //'updated_since' => $updated_since
                //'phase_id' => '2f1e817d-ae56-0703-a95d-1e7215397d16'
            ]
        ];

        if ($manual) {
            $this->alreadySync = false;
            $filter = [];
        }

        $report = ["status" => "already sync"];
        if($this->alreadySync) return $report;

        $report = ["status" => "sync"];

        $dealsTeamleader = $this->getApiResult('deals.list', $filter);


                $deal = TeamleaderDeal::where(
                    "teamleader_id", "=", $cValue["id"]
                )->count();

                if ($deal == 0) {
                    $report["store"][] = $this->store($cValue["id"]);
                } else {
                    $report["update"][] = $this->update($cValue["id"]);
                }

            }
        }

        $this->dealSyncDate->update([
            'deal_sync_date' => Carbon::now()->format('c'),
        ]);

        return $report;
    }
}
tykus's avatar

I mean, that generally for CLI (a queued job will be executed by PHP CLI), the max_execution_time is 0 (unlimited).

Are you using the sync queue driver, so the job executes in the context of the web request?

GodziLaravel's avatar

@tykus Thanks for your support

I use database queue driver :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Queue Connection Name
    |--------------------------------------------------------------------------
    |
    | Laravel's queue API supports an assortment of back-ends via a single
    | API, giving you convenient access to each back-end using the same
    | syntax for every one. Here you may define a default connection.
    |
    */

    'default' => env('QUEUE_CONNECTION', 'sync'),

    /*
    |--------------------------------------------------------------------------
    | Queue Connections
    |--------------------------------------------------------------------------
    |
    | Here you may configure the connection information for each server that
    | is used by your application. A default configuration has been added
    | for each back-end shipped with Laravel. You are free to add more.
    |
    | Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null"
    |
    */

    'connections' => [

        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],

        'beanstalkd' => [
            'driver' => 'beanstalkd',
            'host' => 'localhost',
            'queue' => 'default',
            'retry_after' => 90,
            'block_for' => 0,
        ],

        'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
            'queue' => env('SQS_QUEUE', 'your-queue-name'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Failed Queue Jobs
    |--------------------------------------------------------------------------
    |
    | These options configure the behavior of failed queue job logging so you
    | can control which database and table are used to store the jobs that
    | have failed. You may change them to any database / table you wish.
    |
    */

    'failed' => [
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => 'failed_jobs',
    ],

];

tykus's avatar

And you're running the queue worker? Where is the timeout occurring?

GodziLaravel's avatar

@tykus now i'm working with a simple test API with only 30 elements so I have no problem , but with the real API I have 4000.

tykus's avatar
tykus
Best Answer
Level 104

As I mentioned previously, unlike a web request, a CLI process will run until end or error.

Please or to participate in this conversation.