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.
Related
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..
Basically, I want to remove all the web postings that a user has made via my Android application in the event the user uninstalls my app. Is that even possible?
No you cannot do that, as the app cannot understand when the application is being uninstalled.
If the data to be deleted from the server is such that the user will want it removed, you could make a button in the code, for the user to press, which will invoke process of deleted data on the server. Else just in un-installation, you will not be able to do it
I suggest since your talking about server side code write in some logic that checks the last update date/time on the records and then email the user.
Something to the effect that if they do not click on a link within X days their account will be deleted/purged. If they click the link then just update a single records last update date/time.
See http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REMOVED
The package does NOT receive the intent that it is being removed.
I am creating an online judge. I am using the IDEONE API to compile the file. So when my user uploads a file, there is a function which takes the file and compiles the file using the IDEONE API. But the problem is while the file is being compiled, if my user clicks on a new link, the compilation remains half-finished. Is there a way to make sure the function executes completely before the user redirects?
My first idea was to create a thread and make the thread do the compilation. And while the thread is running, I could show the user a new view. But apparently you cannot run a thread in PHP, so I am out of ideas.
In PHP, there's really no way to make sure a user stays on the page while something completes.
Your best bet is pass your compilation job to a background process. One way to do it would be to insert a row into a database with the information you'd like to process. You can them immediately show the user a message stating their job has been placed in the queue, and will be processed soon. Then set up a Cron to grab the information out of the database, and make the request to your API. When it's done, you update the database again to indicate the job is done. If it's a particularly long running process, you can email the user that their job has completed so they don't have to sit and wait.
Overall your goal is give the user good feedback about what's happening while not running your process in a way that can be lost by the user navigating away.
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.
I would like to find a way to have my user not having to wait for the output of a php script and being redirected to a page while the script is running on the server.
Basically the user submits a form which takes quite long to process and I would like to redirect the user to a page notifying him that the form is being processed and that its output will be later available (I thought about opening a tab when the output is ready).
Basically I would like something like this, which I tried without success, the
if ($form_valid) {
process_form(); // this would need not to be running on the current page so that the user don't have to wait for it to be ready (timeout problems)
header('Location: http://example.com/form_submitted_output_coming_soon.html');
}
I hope that it is not too vague.
Thank you in advance for any help / advice on how I could do that.
It really depends on the time the script takes to execute if it's seconds, under 10 I would do an ajax request and have a modal progress message
If they take extended amounts of time my approach would be to create or use an existing task scheduler/ report generator
a single system scheduled task entry calling a central management script ( probably not optimal )
You mark a task/report for execution
Concurrency. Count, limit the number currently executing ( so you don't over load the server)
users pool via ajax for their tasks / reports or push to the clients with web sockets
Example on how to fork php to background
Update
I think you would get better performance out of a bot continuously check a database or file for work to do and submitting results back to the database. Alerting users via ajax, web sockets and or email when the work that they need is done / updated.
Here is a good introduction on how to build a web crawler in php
The best approach for solving this kind of problem is to use AJAX to make the request to the server in the background and then update the user once it has finished processing.
You may submit the form with an asynchronous request (ajax) and handle the page forward also with javascript. This way your form is handled asynchronously - you may even wait for the response to tell the user once you have an answer. This asynchronous request will not block the UI.
Just for completeness if you really really want to use php only for this:
Run PHP Task Asynchronously