how to stop a function in laravel controller - php

I'm new to LARAVEL. I want to send an AJAX request to a function of controller that this function will run very big loop (takes about 2 minutes). I want to have a button that when click on it, it send a request with AJAX to the controller that stops the function that is running big loop. any idea?

After second click (to stop function) you can send to database information about it. In one of 10 loops function can check database and if function get message "stop" then will stopped.

Related

Get second ajax call response before previous ajax call

I have an ajax call which is used to perform a task and correspondingly update a table with the progress status.Now i have another ajax call to fetch the status from the table to show the task progress status to the user.The second call gets called repeatedly for every 2 seconds until the task is complete.But i could not show the task progress status to the user as the second ajax call keeps on loading till the response comes for the first ajax call.
I found a similar question in stackoverflow in the following link and it was suggested that it might be due to session lock and that using session_write_close() might work.I tried it and found no success.
Prevent jQuery AJAX call from waiting for previous call to complete
Is there a way to get the response for the second ajax call even while the first ajax call is still processing?
You can controller your Ajax functions flow using deferring concept of jQuery like
var defered = jQuery.Deferred();
function your_function(){
//do something!
defered.reslove()
return defered.promise
}
your_function.then(function(){
//this will not run till your_function is done
})
learn more at https://api.jquery.com/jQuery.Deferred/

How to run a background function in codeigniter?

I am using codeigniter with REST API for web service.
I want to send multiple apple push notification in an certain action. The problem is its taking time to response because its sending all the notification and response the web service call.
I need web service call should response immediately and then it will send notification in background.
Any ideas ???
Assign a common function in common.js ,then trigger a function for every 2 min and run it in background as ajax function
window.setInterval(function(){
/// call your function here
}, 5000); // for every 5 sec

Push update notifications back to PHP while calling a function

I have a PHP script that loads a function which uses Curl to log itself in to another webpage to get some data. This operation takes about 14 seconds altogether and some users might become impatient. I have a little busy loader to indicate activity.
I have seen this on other websites and want to know what technology I need to implement the following:
How can I send little notification messages back to the main site while the PHP function is running so the user knows about the progress.
Messages could be in the following form:
Logging in to website
Extracting data
Sorting data
Closing connection
You can do it using ajax, do the following.
1) Keep your curl code in the seperate php file.
2) While click the button, using js or jquery call that php file using ajax.
3) Before ajax return the reponse, you can display the div with your message, once you
got response, you can hide the div.

How to stop a jQuery called php function when user clicks link

I'm using jQuery to call php functions. This works fine except that when a user clicks on a link (if the PHP function hasn't completed executing), the new page will not start loading until the PHP function finishes.
Is there a setting in jQuery I need to change?
Are you using session handling in your PHP code?
If 2 PHP processes are running using the same session, one process will block (wait) until the second one finishes.
Try calling session_write_close() as soon as possible to prevent this from happening.
See http://php.net/manual/en/function.session-write-close.php for more details.

HOW TO make test.php continue with rest of coding without waiting for a function to complete task?

This is the case. At test.php, I have a function dotask(a,b,c,d);
This function need to do task that need 2-4 minutes to complete.
Task including insert new record into db, curl call to other url to do task and etc.
However, I want test.php to:
Just make sure dotask(a,b,c,d) is called, no need to wait until task completed then only return and continue with the remaining code at bottom.
Reason: test.php is a thank you page. I can't expect the user to wait few minutes for the task to be completed. Because I need to show thank you messages etc at that page to user.
What can I do?
You can't fork a process in PHP without a lot of hackery. I'd recommend using a queue and worker pattern instead. See this answer: PHP- Need a cron for back site processing on user signup... (or fork process)
I seen a few solutions here that require you to pass in the page that you want to run, eg:
BackgroundProcess::fork('process_user.php');
But my case is at test.php, I have dotask(a,b,c,d)
I need to pass in parameters from this site to continue the work.
So should I just pass in this few parameter into a new dbtable call pendingprocess, then at process_user.php , I read from database and continue the task instead?
I can't embed parameter into taskpage.php right...
Another solution I can think of is at thankyou.php I do a body onload ajax call by passing in parameter to process_user.php to perform task. Anyone can advice whether this is a good way?
Will the process stop executing when user click STOP at browser? What if they go to refresh the browser.

Categories