Running php page as background without cronjob - php

First greetings to all. Secondly I'm sorry I don't provide source or something but it is because simply I don't have yet.
I just confronted the following issue and need some help and some starting points.
I have a form which submit some data in mysql and save status pending(0) and is working great. What I wonder how can be done is after user submit the form how to trigger another php file which must do following actions on background
pull data from api periodically and check for status changes. Once
the status is changed to Not Pending to update database with new
status.
How can be done this? And to be more fun (for me) this script will update orders, so there would be multiple orders.. Also I'm limited from my hosting provider to not use cronjobs and js..

Related

Loop through a large number of requests via PHP

I am developing a WordPress plugin that fetch user Instagram Profile info and store in database via WordPress wp_remote_get() function. A Corn job runs after every 24 hours that update the user's Instagram Info on daily basis. The problem is I've about 5000+ users and the script runs too long and the task never completed. Everything was working great when users quantity was under < 1000.
Which PHP settings in php.ini should I change to solve this issue? I've set max_execution_time to 0. Any other setting? Any suggestions?
I advice you to do the folowing
create more than one cron job which call same file
after update the user .. mark him as updated
do not update any user if he is marked as updated
make the updating function as transaction (finsh all or cancel all)
finally increase time out also good
Hard to make a valid recommendation without knowing your specific scenario, but I would change your code in the way that it visits instagram profile only when needed as opposed to for everyone via cron job. First, the info will be 'fresher', second, you'll avoid having the problem you're describing.
For instance, when a user visits their profile, a call is made to Instagram and data is pulled. You store the data in your database the same way as before, only with a timestamp. Also in the code, make sure that it doesn't pull data unless it's been 24 hours since the last refresh. Hope this helps.

codeigniter trigger background process

I am working on a module in PyroCMS which uses Codeigniter. In this module users submit some sort of order at front-end and the moderator of the site will be notified and checks the order at the back-end, then he updates the row and the user is notified about the change. All done right now.
I am going to make things a little automatic. The process of supplying the order which is an electronic good like document file, etc is to search the other server ( ftp ) for the order and bring it to the main server.
So, I propose this workflow:
insert the row to the table ( front-end )
initialize the robot to search the ftp server and set a flag that the
robot is processing the order.
if found: transfer the file to the server and update the row and
flag.
if not found update the flag and let the manager do the rest of the
job.
Now, The thing is that I think it is not a good idea to put the robot code into the same controller and fire the robotic task with the http request from user. I mean, the form is submitted and I just want to insert the row and end the process and notify the user that it is submitted, then the robotic task should be done at the background.
Now we can update the workflow like:
insert the new row
notify the user that your order is submitted. and let user go.
run the background process (trigger it) to search the ftp server and
update the row upon success or do nothing upon error.
how can I do this type of background proccess? any idea or experience?
You can put your robot code to other php file and then run that php file using the php's system or exec command after successfull form submission. This will run like new php thread.
Hope this helps.

Display realtime user status using AJAX and PHP from a MySQL database

I have a simple mysql query which shows users last status. I now want to go further and create a page to show users most recent status without refreshing. The mysql DB is dynamically updated with the users status.
So all that is required is for the page rendering the mysql to refresh with the latest user status without me having to hit the refresh button.
Could anyone provide guidance on how I go about doing this. Thanks
You need push into page, not pool. It is kind of stupid to do poling from database.
Here is how that works.
Let asume that you have n users on you page.
Every time that status is changed (you insert that into you DB)
2.1 As this happens you need to push signal to your webpage (use Strophe library).
2.2 In order to push something to page you need Strophe instance running.
2.3 If your website is PHP, here is good class for communicating with Strophe instance.
I can do this for your but you will be more happy if you do this on your own.
That real time stuff are very interesting.

PHP cURL or header approach?

I am confused what approach to take when updating a status e.g delivery on our client's system. On normal process, they provide us URL where we will post back the delivery data.
The problem now is what if we update the delivery data, then we'll have to notify our client's system too so the change of delivery status on their system happens real time instead of the cron job (check delivery status).
Should this be a
header('Location:http://path/to/client/parse_update_delivery_status.php');
once all the updating on our system is done or a
cURL.... //http post to client's url for updating delivery status on their system based on what we passed here
approach?
I'd really appreciate your input! :) Thanks!
I'm pretty certain that would have to be done with cURL, and at the very least would work much better using curl. Does parse_update_delivery_status.php receive data from an HTTP POST?
Using a location header causes the browser to redirect to that script, and no data gets passed along with it (unless you add it to the query string). If an update was performed, a person could potentially stop their browser from loading the redirect by hitting stop fast enough. Also, chances are that script won't output anything meaningful to the user so they would be left with a blank page or data on their screen that they don't understand and would have to use the back button to return to your site.
If the update was run from a cron job or PHP CLI script, then the headers have no meaning anyway.
This should be done as a cURL operation. The reason for this is your update scripts shouldn't be held responsible for handling browser operations like redirects; their job should just be to update whatever they need to update. By using cURL you can move all of your code which handles whatever status codes are returned by the update script into something which presents that data to the user, instead of intermingling that with your update script. By using this approach, you can keep your update script clean and allow it to be called by multiple sources without worrying about misleading redirects.

how can I refresh a webpage using ajax, after database inserts

I have a service running on a pc witch does some inserts in my (MySQL) database. What I want to do is everytime a new record is inserted in database to refresh automatically my webpage (I am using php). I read a relative post about updates
refresh the webpage on database update, but those updates were done "from" the webpage.
I also read another post
https://stackoverflow.com/questions/6460297/automatically-refresh-the-webpage-just-after-a-new-database-entry, didn't figure out how I can do this.
Any suggestions?
This is how you do it
Use JavaScript's settimeout() function to run an ajax request to your server at a set interval in which you send the id of the last record on the page. The use Javascripts window.location.reload() function to reset the page if the last record's id is different.
Why you don't want to refresh the page
This is bad user experience. You don't want the page refreshing out of no where. The best idea is to send the latest id on the current page to the server and check for any new ids. If there are new ideas send the records back via json and append them to the end of your results table.
This scenario is some complex but possible solution of your requirement!
In our scenario we have developed a sms-gateway and defining a trigger when ever any record is inserted we call a function that sends a sms to our gate way that behind that gateway we have developed our page. :)
In other way by defining timing ajax or many other ways where some what how our some resources are used, we have to compromise.
Ideally you want to trigger an update of data on the page via AJAX as opposed to a full page refresh. You can potentially accomplish this using web sockets. A popular server-side implementation is socket.io. Which uses the nodejs environment.
You could potentially write a MySQL UDF which executes in your trigger and signals nodejs to push more data (which may require writing a nodejs package as well). So, not trivial, but definitely doable :)

Categories