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.
Related
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.
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've a complex php cron job that retrieve data from an external webpage and join all the information in one variable that is encoded in json. The whole process is very slow and takes a lot of time.
The point is that I need to retrieve the json object from my index page, but I don't want to load all the script because it will take too long to execute. What I've been doing is tell the cron job to create a new file and write the json object and I've been retrieving the information from that file.
I would like to know if there is a more efficient/simple way to transfer this information without having to create a new file or executing the script 'manually'. I've heard that you can send information using CURL, the truth is that I've never used this technique before, so dunno if it would be useful in this situation.
This is a pretty common issue. Long running tasks shouldn't be executed on page load because it impacts ux. having your time intensive php script running as cron job is a great solution.
Perhaps using a db would be easier still. You can easily using sqlite or a "full fledged" rdbms to store your data (like mysql or postregs). it could be something like:
time intesive php script is running on cronjob every x minutes. Saves data to your db instead of a file.
When user requests index page it sends ajax request to another php script. The php script looks for data in your db and returns it to your user if it exists.
I have to create a process call on a db field(s) being a certain status. I have heard you can execute a cURL call with a db trigger but Google is not being kind enough to return anything I can use.
So I guess my question is three parts:
Can this be done?
Reference?
Alternative Solution?
Workflow:
db field is updated with status, need to kick off script/request/process that run the next step in my work flow (This is a PHP script) that will pull the recorded in the db and process another step, then update the db with the results.
You shouldn't use triggers for that, as a trigger blocks transactions so it will make your database very slow. Also you'd need to install unsafe language to Postgres — pl/sh, pl/perl, pl/python or other.
There are 2 better solutions for this problem:
have a process which connects to database and LISTENs for NOTIFY events generated by your trigger — this will work instantly;
periodically check for new data using, for example, a cron script - this would work with a delay.
If you can call a shell script,
http://plsh.projects.postgresql.org/
you can call a curl.
But I get a creepy feeling about the approach...
If the remote server goes offline, data inconsistency??
Alternative:
I wouldn't put business logic in triggers, only customized constraints or denormalisation.
Do what you need to do with either middle-tier, or stored procedures.
Regards,
//t
I think what you're looking for is a trigger in postgres that will run the necessery script. Triggers are explained in the documentation, the syntax for adding a new trigger is explained here. The trigger type you're looking for appears to be an AFTER UPDATE trigger. As far as I know, the script you run will have to check if the field is of the required status, as postgres will always run the trigger.
I have done some google search on this topic and couldn't find the answer to my question.
What I want to achieve is the following:
the client make an asynchronous call to a function in the server
the server runs that function in the background (because that function is time consuming), and the client is not hanging in the meantime
the client constantly make a call to the server requesting the status of the background job
Can you please give me some advices on resolving my issue?
Thank you very much! ^-^
You are not specifying what language the asynchronous call is in, but I'm assuming PHP on both ends.
I think the most elegant way would be this:
HTML page loads, defines a random key for the operation (e.g. using rand() or an already available session ID [be careful though that the same user could be starting two operations])
HTML page makes Ajax call to PHP script to start_process.php
start_process.php executes exec /path/to/scriptname.php to start the process; see the User Contributed Notes on exec() on suggestions how to start a process in the background. Which one is the right for you, depends mainly on your OS.
long_process.php frequently writes its status into a status file, named after the random key that your Ajax page generated
HTML page makes frequent calls to show_status.php that reads out the status file, and returns the progress.
Have a google for long running php processes (be warned that there's a lot of bad advice out there on the topic - including the note referred to by Pekka - this will work on Microsoft but will fail in unpredicatable ways on anything else).
You could develop a service which responds to requests over a socket (your client would use fsockopen to connect) - some simple ways of acheiving this would be to use Aleksey Zapparov's Socket server (http://www.phpclasses.org/browse/package/5758.html) which handles requests coming in via a socket however since this runs as a single thread it may not be very appropriate for something which requiers a lot of processing. ALternatively, if you are using a non-Microsoft system then yo could hang your script off [x]inetd however, you'll need to do some clever stuff to prevent it terminating when the client disconnects.
To keep the thing running after your client disconnects then the PHP code must be running from the standalone PHP executable (not via the webserver) Spawn a process in a new process group (see posix_setsid() and pcntl_fork()). To enable the client to come back and check on progress, the easiest way to achieve this is to configure the server to write out its status to somewhere the client can read.
C.
Ajax call run method longRunningMethod() and get back an idendifier (e.g an id)
Server runs the method, and sets key in e.g. sharedmem
Client calls checkTask(id)
server lookup the key in sharedmem and check for ready status
[repeat 3 & 4 until 5 is finished]
longRunningMethod is finished and sets state to finished in sharedmem.
All Ajax calls are per definition asynchronous.
You could (although not a strictly necessary step) use AJAX to instantiate the call, and the script could then create a reference to the status of the background job in shared memory (or even a temporary entry in an SQL table, or even a temp file), in the form of a unique job id.
The script could then kick off your background process and immediately return the job ID to the client.
The client could then call the server repeatedly (via another AJAX interface, for example) to query the status of the job, e.g. "in progress", "complete".
If the background process to be executed is itself written in PHP (e.g. a command line PHP script) then you could pass the job id to it and it could provide meaningful progress updates back to the client (by writing to the same shared memory area, or database table).
If the process to executed it's not itself written in PHP then I suggest wrapping it in a command line PHP script so that it can monitor when the process being executed has finished running (and check the output to see if was successful) and update the status entry for that task appropriately.
Note: Using shared memory for this is best practice, but may not be available if you are using shared hosting, for example. Don't forget you want to have a means to clean up old status entries, so I would store "started_on"/"completed_on" timestamps values for each one, and have it delete entries for stale data (e.g. that have a completed_on timestamp of more than X minutes - and, ideally, that also checks for jobs that started some time ago but were never marked as completed and raises an alert about them).