JimNayzium's avatar

PSR-1 Question

I assume that altering the execution time for a class's main function, like increasing the .ini from 60 seconds to 300 seconds just for that JOB's handle() function would be considered a breaking of the PSR-1 standard of either havng side effects or declarations, but not both.

So my question becomes, if my application has complexities that vary wildly, and one of the jobs that will run daily will error out if not given a 300 second time limit, even after many many refactors, and breaking it down numerous times, what would be the best way to go about handling this type of required issue?

Should my .ini sitewide settings just be 300? That seems like massive overkill when only a few jobs that process much more complex things require it.

Maybe having a dedicated WORKER that has the flag of execution time set to 300 for that job solely is the way to go? If so, can someone show me how I would setup different workers for different jobs in my scheduled tasks?

THANKS

0 likes
2 replies
LaryAI's avatar

Great question! First, let’s clarify the PSR-1 concern:
PSR-1 is about basic coding standards (file structure, class naming, etc.), not runtime configuration like max_execution_time. Adjusting PHP’s execution time does not violate PSR-1, as it’s not a declaration or side effect in the sense PSR-1 describes.

Best Practice for Long-Running Jobs

You’re correct that setting a high max_execution_time globally is overkill and not ideal. The best approach is to run long-running jobs on dedicated queue workers with their own configuration.

How to Set Up Dedicated Workers in Laravel

Suppose you have a job that needs more time. Here’s how you can handle it:

  1. Use a Separate Queue Connection or Queue Name

    In your config/queue.php, you can define a new connection or just use a different queue name.

  2. Dispatch the Job to a Specific Queue

    // Dispatch the job to the 'long-running' queue
    LongRunningJob::dispatch()->onQueue('long-running');
    
  3. Start a Worker with Custom PHP Settings

    You can start a worker for that queue with a higher max_execution_time:

    php -d max_execution_time=300 artisan queue:work --queue=long-running
    

    Or, if you use Supervisor to manage workers, your config might look like:

    [program:laravel-long-running]
    process_name=%(program_name)s_%(process_num)02d
    command=php -d max_execution_time=300 /path/to/artisan queue:work --queue=long-running --sleep=3 --tries=3
    autostart=true
    autorestart=true
    user=youruser
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/path/to/your/worker.log
    
  4. Keep Your Default Workers Fast

    Your default worker can keep the normal max_execution_time and process all other jobs.

Summary

  • Don’t change sitewide settings just for a few jobs.
  • Use dedicated workers (with Supervisor or similar) for jobs that need more time.
  • Dispatch those jobs to a specific queue.

This approach is scalable, maintainable, and doesn’t violate any PSR standards.

Let me know if you need a Supervisor config example or more details!

Please or to participate in this conversation.