Below is my flow:
I have one PHP (override.php) page: this page calls one PowerShell script with 2 dynamic (id,scriptName) arguments(CRON.ps1).
In CRON.PS1 I am scheduling one task scheduler to call next_trigger.php along with these 2 dynamic arguments in the time span of 2mins. it is creating task scheduler successfully for the PHP page next_trigger.php id scriptName
In next_trigger.php, I have created an AJAX call to redirect to the particular scriptName(argv[2]) and I am updating DB there.
This is my flow of Automation. (It is just a module of my Automation. Actually, it's massive automation, if it's confusing I'm ready to explain again).
Issue:
While I hard code the value(argv1,argv[2]) in next_trigger.php its working fine and updating in DB.
But if I dynamically call from the task scheduler it's not going to the ajax call. till ajax call it's working fine and doing the operations, no issues in fetching the argv values
While I searching I get one stackOverflow link 'Run PHP/Javascript/AJAX Application On Schedule/Cron?'
but not getting the exact thing why it is not possible.
Other way I tried
Instead of next_trigger.php, I scheduled one batch file with 2 argv.
In that batch file, I wrote those values in a .txt file, then called next_trigger.php
In next_trigger.php I read the .txt file and calling the ajax. [same: till ajax its working fine, not calling ajax.]
I am not sure, may be because of without user intervention it is happening, or Am I missing any important thing here. It's really eating my entire time. Please help me to understand the possibilities.
Related
On website i have php script with needs to be called in order to collect data and set to array from which it sends to mySql. I have tried cron job calling it with www.website.com/script/sc1.php?value="val" and the cron says it is done and ok, but it's not (the data is not inserted in mySql).
Is there any other way for me to do that or should i use task scheduler on windows or setup a program java or c++ to call the webpage in certain time (it's quite a bother so I would prefer an easier approach to the issue.
To recap: Cron is not working or rather wont execute the call (it's done in about 10-12sec due to high amount of data)
Opening it in browser does work and it executes without issue.
I have tried using cron-job.org and the response i get for the call is:
Sorry for edits, the call is passed into switch statement and if val == any the proper function is called to insert data into mySql.
I have a form which when submitted, calls a php page (sample.php)
my php page does a lot of execution, which takes around 5 mins of time. i am also printing "Executed!" on my sample.php page.
This Executed gets printed.. only after it has executed everything ( 5mins).
I want my php page to print "Executed" before it does all the processing.
How shuld i go about this?
There have been several solutions posted that use ignore_user_abort() and flush() to continue background work after a page has been delivered to the client. You should start reading the documentation on connection handling on the php web site
However, if you ask for a stable solution, I would design your application in a way that 'sample.php' (the form action) will just recieve a job, adds it to a queue (maybe a database table) and reports that the job has been added. Where another process runs in background (maybe per cron or whatever) and runs the jobs itself. Also I would create a page like 'progress.php' where the progress of a job can be viewed. The response could be json or something like this, so that it can be easily integrated into other pages or used as data feed for the javascript progress bar you've been asked for.
I am struggling with something.
I have an PHP page that does an ajax call to another page using jQuery $.ajax. It sends the request async to the processing page which then returns a response.
This works fine now but we are making some changes to the backend and the processing (SQL stored procedure) that runs is now taking a lot longer like well over 5 minutes. The wait is is fine because we are dealing with close to 200MM records in SQL.
The thing is I need to be able to send the request to the processing page and not have to wait for a response. The processing page fires off the stored procedure in PHP like this:
$query = $dbh2->prepare('exec sp_name :countID');
$query->bindParam('countID', $countID);
$query->execute();
Now again that stored procedure takes awhile to run and we do not need the results of that to be presented back to the user. There is though some additional PHP code that needs to run after the stored procedure but again nothing needs to be send back to the browser.
I am trying to figure out a way that I can make a call to the processing page and it runs the stored procedure and the other code but the user's browser does not need to wait for the response. Right now if the try to click off the page too soon it basically locks up the browser for awhile and does not finish the processing.
Any insight into this would be great.
Thanks in advance for any help.
Sequenzia, if I understand correctly, then I've been here and found a way through this quagmire after a lot of research.
I provided an answer to a similar question a few months ago. Unfortunately, the OP nor anyone else has ever accepted/commented/upvoted/downvoted - nada.
Run a batch file from my website
And here are some useful references :
Running a background script (unix command)
Ref: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/
Ref: http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html
How to compose PHP $shortopts and $longopts
This is the way to interpret parameters passed to a PHP script when run from the command-line, or from another PHP script with shell-exec()
Ref: http://www.php.net/manual/en/function.getopt.php
You might look at setting the timeout option for the $.ajax() method. By setting a timeout of maybe half a second or whatever, the ajax will just timeout and go into the error handler (if any).
I need to create an event listener. I'm a novice so be kind :)
Basically I am on page1.php (php file); I want inside a loop to go check page2.xml (xml file) for some information which should be received at some point. Either check it all the time, or wait and every 5 minutes or so to see if some information has been received there. Either of them work for me.
If no info has been received after a few minutes, then I want to run again the loop (until it is received), otherwise, move forward and do something with my newly received information. This part I have no problem with, just the event listener itself. I couldn't find the function I should be using anywhere. :( I only need to check and retrieve the content of the xml file every so often.
I am not so sure how I should go about this if there isn't just a function which does this, but I couldn't find much when I searched for "event listener php".
Any help would be appreciated: reference to tutorials/sample code/even just telling me what keywords I should be looking for or what I need to learn first in order to do this.
Thanks!
Well, first you should understand the terminology you're using. PHP is not an event-driven language, it is a request-driven language. A request comes into the web-server, PHP parses it and a response is sent back to the requester. At no point are there events triggered that you can process or handle. You can implement your own "event system" but ultimately this is much more work than what your use-case entails.
Your best bet is likely utilizing AJAX and continuously making requests to your PHP script until you return the data that you are looking for. Ultimately you will need to learn about the XMLHttpRequest JavaScript object. After you understand how to make asynchronous requests utilizing JavaScript you can look at the setInterval() method for how to repeatedly make a request.
Once you can repeatedly make asynchronous requests it should be a relatively simple process of creating a webpage where you can trigger the AJAX requests to be sent.
There is no need for a loop in your PHP code. The loop is effectively done on the other end. Here's a textual workflow that you might follow:
Go to a site designed to trigger your AJAX calls and trigger them.
Make your async request to your PHP script.
Inside your PHP script open up the XML file and check for the necessary content.
Return a response in the form of a JSON object. One response can mean the data wasn't updated, the other response means the data was updated.
Parse the response, if the data was not updated repeat from step (2). If the data was updated continue to step (6).
Display a celebratory greeting that your data was updated or a notice that we are still waiting for the data to be updated. Perhaps you can have the number of tries as well, off to the side.
I did the following:
Automatically saved to database every time something new came in.
Then ran a php loop that every few minutes checked to see if there is something new in the database which fits the parameters of this new event (including that it happenned within the timeframe of the past few minutes). I used flush(); and then sleep(120); in the loop to get the loop to keep running every few minutes, until the new info came in in which case it will break(); or die();.
I did something like this writing an inbox parser in PHP. You're best option is to:
Code page1.php in which you just need to do 2 things: read XML from page2.xml and
if there is something "new" just execute the data-parsing
code.
Setup a Cron job (if you're under linux) to execute every 5 minutes or so (Cron command is something like: php /path/to/page1.php). In the same way, if you're running Windows you can setup a scheduled task and execute the same command. Be aware that the full path to your PHP installation should be in PATH environment variable.
I've googled for this but didn't find any solution - is there a way to create a progessbar for adding/extracting files to/from zip archive in PHP?
Can I get some kind of status message which I can than get with an AJAX request and update the progress bar?
Thanks.
I am trying to do the same thing at the moment; it is mostly* complete (*see issues section below).
The basic concept I use is to have 2 files/processes:
Scheduler (starts task and can be called to get updates)
Task (actually completes the task of zipping)
The Scheduler will:
Create a unique update token and save to cache (APC)
Call the Task page using curl_multi_exec which is asynchronous, passing update_token
Return the token in JSON format
OR
Return the contents of the APC under the update_token (in my case this is a simple status array) as JSON
The Task will:
Update the APC with status, using the update token
Do the actual work :)
Client-side
You'll need some JavaScript to call the Scheduler, get the token in return, then call the Scheduler, passing update_token, to get updates, and then use these returned values to update the HTML.
** Potential pitfalls**
Sessions can be a problem. If you have the same session you will notice that your browser (or is this Apache?) waits for the first request in the session to complete before returning others. This is why I store in the APC.
Current Issues
The problem with the ZipArchive class is that it appears to all the grunt work in the ->close() method, whilst the addFile method appears to take little to no time to complete.
As a workaround you can close and then reopen the archive at specific byte or file intervals. This actually slows the process of zipping down a little, but in my case this is acceptable, as the visual progress bar is better than just waiting with no indication of what is happening.