Long running process in Linux using PHP
Background
To do stuff, I usually create web-based applications written in PHP. Sometimes we need to run something that takes a long time, far longer than the 10 second psychological limit for web pages.
A bit of googling in stack overflow found us this http://stackoverflow.com/questions/2212635/best-way-to-manage-long-running-php-script, but I will tell the similar story with a different solution. One of the long running tasks that need to be run is a Pentaho data integration transformation.
Difficulties in long running PHP scripts
I encountered some problems when trying to make PHP do long running tasks :
- PHP script timeout. This could be solved by running set_time_limit(0); before the long running tasks.
- Memory leaks. The framework I normally use have a bit of memory issues, this can be solved either by patching the framework (ok, it is a bit difficult to do, but I did something similar in the past) or splitting the data to process into several batches. And if you are going to loop the batches in one PHP run, make sure after each batch there are no dangling reference to the objects processed.
- Browser disconnects in Apache-PHP environment would terminate the PHP script. During my explorations I found that :
- Some firewall usually disconnects a HTTP connection after 60 seconds.
- Firefox have a long timeout (300 seconds or something, ref here)
- Chrome have timeout similar to Firefox (about 300, ref here), and longer for AJAX (stackoverflow ref doesnt timeout after 15 hours)
- Difficulties in running pentaho transformations, because the PHP module would run as www-data, and will be unable to access the kettle repository stored in another user's home directory.
Workarounds
I have experiences using these workarounds to force PHP to be able to do long running web pages :
- Workaround 1 : use set_time_limit(0); and ignore_user_abort(true); to ensure script keeps running even after client disconnects. Unfortunately the user will no longer see the result of our script.
- Workaround 2 : use HTTPS so the firewall will unable to do layer 7 processing and doesn't dare disconnect the connection. If the user closed the browser then the script would still terminate, except when you also do workaround 1.
I haven't tried detaching a child process yet like , but my other solutions involve separate process for background processing with similar benefits.
Solution A - Polling task tables using cron
It is better to separate the user interface part (PHP web script) with the background processing part. My first solution is to create cron task that are run every 3 minutes, which runs a PHP CLI script which checks a background task table for tasks with state 'SUBMITTED'. Upon processing the task, the script should update the state to 'PROCESSING'.
So the user interface/ front end only checks the background task table, and when the user orders to, inserts a task there with the specification required by the task, setting the state to 'SUBMITTED'.
When cron gets to run the PHP CLI script, it would check for tasks, and if there any, change the first task state to PROCESSING and begin processing. When processing complete, the PHP CLI script would change the state to COMPLETED.
Complications happen, so we will need to do risk management by :
- logging phases of the process in some database table, including warnings that might be issued during processing.
- recording error rows if there is any in another database table, so the user could view problematic rows
Currently this solution works, but recently I came across another solution that might be a better fit for running a Linux process.
Solution B - Using inotifywait and control files
In this solution, I created a control file which contains only one line of CSV. I prepared a PHP CLI script which parses the CSV and executes a long running process, and also a PHP Web page which would write to the control file. Inotifywait from inotify-tools will listen on file system notifications from Linux kernel that are related to changes on the control file.
The scenario is like this :
- User opens PHP web page, and choose parameters for the background task, clicked on Submit
- PHP web page receive the submitted parameters, and write them into the control file, including job id. The user received a page that states 'task submitted'.
- A shell script that running inotifywait, will wait for notifications on the control file, specifically for the close_write event
- After close_write event received, the shell script will continue, and run PHP CLI script to do the background processing
- PHP CLI script reads the control file for parameters and job id
- PHP CLI script executes linux process, redirecting the output to a file identified by job id in a specific directory
- The web page that states 'Task Submitted' could periodically poll the output file with the job id, and shows the output to the end user (OK, this one I need to actually try later)
- PHP CLI returns, the shell script performs an endless loop by going to (3)
Conclusion
By using Linux file system notifications, we could trigger task execution with parameter specified from a PHP web page. The task could be run as another Linux user, provider the user running the shell script. Data sanitization are done by php, so no strange commands could be passed to the background task.
These solutions are written entirely in open source solutions. I saw that Azure have WebJobs which might fulfill similar requirements that I have, only it is in Azure platform which I never used.
Comments
data science course malaysia
data analytics courses
360DigiTMG
360DigiTMG data analytics course malaysia
360DigiTMG best data science certification course
360DigiTMG tableau classroom training
360DigiTMG
Tinder Gold Apk
Share more
Gaudeo
Kitsunemusicacademy
Corejoomla
Data Science Course in Bangalore
Data Science Training in Bangalore
Good to see it.
data science training in aurangabad
data science course
data science course in hyderabad
data science training
data science institute un hyderabad
data science training in hyderabad
data science
Data science course in Mumbai
Business Analytics course
data science courses in noida
hello sir,
thanks for giving that type of information. I am really happy to visit your blog.Leading Solar company in Andhra Pradesh
data scientist course noida
data analytics training in yelahanka
Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
1000 Backlink at cheapest
50 High Quality Backlinks for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest
máy xông tinh dầu bằng điện
máy khuếch tán tinh dầu silent night
máy xông tinh dầu đuổi muỗi
máy khuếch tán tinh dầu hà nội
data scientist training and placement in hyderabad
https://serijeonlines.net
data scientist training and placement
data scientist training and placement in hyderabad
https://avanga.vn/khai-niem-than-so-hoc-la-gi/
Ý nghĩa số 2 trong biểu đồ ngày sinh
Ý nghĩa số 3 trong biểu đồ ngày sinh
Ý nghĩa con số chủ đạo 2 trong thần số học
Best Gym in Visakhapatnam
Great to become visiting your weblog once more, it has been a very long time for me. Pleasantly this article i've been sat tight for such a long time. I will require this post to add up to my task in the school, and it has identical subject along with your review. Much appreciated, great offer. data science course in nagpur
Best Gym in Visakhapatnam
Fitness Gym in Visakhapatnam
apk world
latest version
updated version
No Root
extractor apk
apkstick
emiratesids.blogspot.com
emiratesidcards.blogspot.com
The best reputable and quality food container bag
What is the application of 4 loops of bulk Tote Bags?
Preeminent advantages of waterproof jumbo bags
3 things to know about 500kg 1000kg baffled bulk bag
bao fibc
bao jumbo 1000kg
công ty bao bì jumbo
If you want to know about how to make Yahoo my homepage on Chrome then open Google Chrome and tap on settings option. Toggle the bar for the option show home button under the appearance section. After that, click on enter custom web address option under show home button option. Choose the address that you want to open while clicking on home button. Here, enter Yahoo page that you want to set as homepage. At the bottom, you will see the option for opening a specific page or set of pages. Tap on add after entering the address that you want to see once Chrome launches. Lastly, you can smoothly add multiple Yahoo pages and it will open in separate tabs.
How to Turn Off Outlook Notifications on Phone?
Check out the steps mentioned in the guide and know how to turn off Outlook notifications on phone. If you are an Android Gmail user, then start by opening the Gmail application and tapping on the local left menu button. After that, scroll down to the bottom and choose Settings. Lastly, click to account and then uncheck the notifications. These are the steps that users need to follow to smoothly turn off the Outlook notifications on their mobile phone. It is advisable to follow the steps properly to perform the function properly.
How to Deal with Microsoft Outlook Search Not Working?
Sometimes third-party add-ins built to boost the user’s productivity and because of this, users encounter Microsoft Outlook search not working issue. If you want to know about the steps to disable add-ins then start by launching Outlook and choose your File tab. Here, click on options and then click on add-ins from the list. A list of the add-ins includes the active, inactive, and disabled add-ins will appear on the screen. Choose COM add-ins from the manage list and tap to go. Here, untick the add-ins that you want to disable and choose to OK. Lastly, you need to see the effect of disabling the add-ins and then you need to restart the Outlook application.
watch wrestling
Watch Pro Wrestling
Ehsaas Rashan Program 2023 Online
Ehsaas Program BISP 8171