简体   繁体   中英

Heroku PHP background worker

In my app in heroku I have a task that needs more than 30 seconds to execute and I can't increase the execution time because heroku won't allow that. I take data from an API in the front-end and then send it using AJAX to the server to insert this data to the database. This process in the server takes more than 30 seconds to complete and it gives me a timeout error.

I created the Procfile as below : worker: cd ~/www/ && ~/php/bin/php worker.php

The file worker.php takes the data sent using AJAX and then inserts it into the database.

I enabled the worker in the heroku dashboard

What I want to achieve is start this worker only when I click a button that start the ajax call. I don't want to use heroku scheduler because this script will only be run once a month or whenever the client wants to add new products. Also if I use the scheduler I don't know how to send the data from the API to the server.

If this isn't possible is there a way to force heroku to increase the execution time?

Thank You

What you need is to optimise your sql queries to reduce the execution time instead.

According the description of your comment, the action you trying to do is "UPSERT" - update the existing row or insert if not exist.

Try google sql upsert and you will find more information. NOTE: upsert can means several different situation and each need to tackle differently. read this article which helped me much on sql upsert

Other consideration is drop unnecessary indexes for the table which makes insert even slower as it re-index for every insertion if not bundled. If you are still in development that applies to unnecessary foreign key as well.

Another side note, in another post it said normally sql can handle ~40k insert/minutes, so if you are inserting more than 15-20k, which is about 30s you probably should consider a private server or maybe upgraded heroku allow more resource or execution time that may suit your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM