0

I'm trying to stop the execution of an php script in runtime triggered by the user.

My planned approach is to make an ajax call to the server, setting a session variable which is then looked up by the first already running script every now and then to determine if the process should stop or continue running. Hope that makes sense:)
I already found some examples of people doing the same here or with files instead of sessions here.

Now my problem is that I would like to use the session for my approach instead of the solution doing it with temporary files and the mentioned approach with sessions doesn't seem to work for me in Laravel 9.

The result I'm looking for:

  1. start first php script (runs maybe 30 seconds)
  2. make ajax call to server & set session variable stop_execution = true
  3. the first php script which is still running detects the change in stop_execution === true & stops execution.

The behaviour I get:

  1. start first php script (runs maybe 30 seconds)
  2. make ajax call to server & set session variable stop_execution = true
  3. the first php script which is still running doesn't detect the change in stop_execution === true & runs until it finishes by itself.
  4. the next time I run the first php script again it immediately detects the change in stop_execution === true & stops execution.

My thought on why this is happening is that the session variables doesn't get refreshed inside the first script after checking them for the first time. Maybe there is a way to force pull all new changes from the session variables while the first script is running? Did somebody have the same issues with Laravel? It seems like this is working with session variables when not using Laravel. I think it has something to do with Laravel and how the sessions are handled.

I would appreciate any advice 😊
Thanks a lot!


Code:

First script executed at the beginning

private function longRunningFunction()
{
    // check session variable every iteration and exit while loop if === true
    while ($condition === true && ! session('cancel_computation')) {
        // do stuff ...
    }
    // reset session variable to false after finishing
    session(['cancel_computation' => false]);

    return $result;
}

Second script executed on ajax call

public function cancelComputationFunction()
{
    // set session variable to be true
    session(['cancel_computation' => true]);
}

1 Answer 1

2

I would not advice you to use sessions for this.

They are initialized on script start and I have never seen somebody re-fetch them in the same script. Nor am I able to find such functionality when researching online.

What you could do though, is to utitlize the Cache facade.

It is very well suited for what you want and it very lightweight no matter which driver you choose to use under the hood in Laravel.

So instead of:

public function cancelComputationFunction()
{
    // set session variable to be true
    session(['cancel_computation' => true]);
}

You could do something like this:

public function cancelComputationFunction()
{
    // set cache variable to be true
    Cache::put('cancel_computation_' . session()->getId(), true);
}

And likewise inside the long-running part of the script.

Sign up to request clarification or add additional context in comments.

4 Comments

Thx for the tip but sadly I get the same result using the Cache facade. I implemented it exactly like you mentioned... Any ideas?
I have only suggested the changes needed in the cancelComputationFunction(). You youself need to change longRunningFunction() to be using cache. Did you do that? And how does that code looks now?
Yes, I did that too... I updated my question to include the changes I made in both files. The behaviour didn't change at all even if I was using the cache facade Cache::put() like you suggested instead of the global cache helper cache() in both files.
I now have found the problem I had the whole time... I was only testing on my local machine and therefore my second ajax request sent for setting the cache variable could only be processed AFTER the first one had finished. That's why i got the same behaviour both ways... Thx for your suggestion to use cache instead of sessions 🙏🏼

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.