Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Job status within transactions #9

Closed
SlyDave opened this issue Nov 24, 2017 · 5 comments
Closed

Updating Job status within transactions #9

SlyDave opened this issue Nov 24, 2017 · 5 comments

Comments

@SlyDave
Copy link

SlyDave commented Nov 24, 2017

Having issues using this with transactions

$count = 100;
$this->setProgressMax($count);

DB::beingTransaction();
try {
    for ($i=1; $i <= $count; $i++) {
        $this->setProgressNow($i) // no easy way to do this outside the transaction! :(
        dump($i);
    }
}
catch (\Exception $e) {
    DB::rollBack();
    $this->setProgressNow(0);
    throw $e;
}
DB::commit();

Obviously, with this set up it goes from 0 to 100, because all progress counter is ticking up inside the transaction which doesn't commit until the end of the loop.

What I'm trying to achieve is a way to tick up the progress, whilst the actual work isn't committed until the end, and if an error occurs, set the progress back.

@imTigger
Copy link
Owner

imTigger commented Dec 7, 2017

DB::beingTransaction() seems to be connection-wide.
There seems no way to commit immediately inside a transaction.
Unless we open another connection, but still need some testing

@SlyDave
Copy link
Author

SlyDave commented Jan 24, 2018

Yeah, this is pretty much going to need a secondary connection to work.
Then it's going to need some finally keyword play to handle errors in the executing job to set the status.

I wonder if it's possible to clone the connection based upon the existing connection configuration, otherwise we enter the territory of having to get people to configure a second connection just to use this - which isn't ideal...

@tpetry
Copy link

tpetry commented Jul 4, 2019

Hey, that's my problem, too: I have a job that performs operations on the database for up to 5 minutes and i would like to provide the user with progress information.

How about alternatively storing job status information in redis instead of the database? This would also solve the efficiency problem that when saving fine-granular Progress updates a lot of database updates take place.

A disadvantage of course is that no eloquent models can be used anymore. But since the model could not be changed anyway (#17) it is probably not a problem at all. There would only have to be a (static) function to get the current status information based on a job ID.

@crashkonijn
Copy link
Contributor

We could do something like this, although I can't really oversee possible side effects of this:

$c1 = app('db.connection');
$c2 = clone $c1;

$c1->transaction(function () use ($c2) {
    $c2->table('job_statuses')->delete(); // this still deletes everything
});

@imTigger
Copy link
Owner

Transaction support is added in 1.1.0-rc1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants