stackkit/laravel-google-cloud-tasks-queue

Tasks are dispatching before DB Commit

RazorMeister opened this issue · 5 comments

Hello.

I've found a bug in this package. When I set jobs to dispatch only after DB commit, tasks are dispatching immediately. I think that the problem is in the CloudTaskQueue file in methods: push and later. They are not using enqueueUsing method from laravel parent queue when the dispatch after db commit feature is implemented.

I think that push method should look like this:
image

Thank you, I'll try and see if I can implement this soon

@RazorMeister This is fixed in v3.2.1!

This is still happening in v3.4.0

I have a repository that creates a model and fires a observer, and this observer send a notification via database, push, broadcast and email. But the "database" doesn't work, because in my repository I have a transaction around the model creation.

It works well if I remove the transaction.

Can you please verify?

I'm unable to reproduce this, can you please share some failing code? It's not mentioned here but just making sure: this feature only works when calling afterCommit(). Without it, Laravel will dispatch the job before the transaction.

I'm using this example:

Executing code

dump('Transaction started.');
    
DB::beginTransaction();

User::create([
    'name' => 'Test',
    'email' => 'test+' . bin2hex(random_bytes(16)) . '@example.com',
    'password' => bcrypt('test'),
]);

dump('Sleeping 3 seconds.');

sleep(3);

dump('Sleeping done.');

dump('Committing transaction.');

DB::commit();

dump('Done.');

UserObserver

class UserObserver
{
    public function created(User $user)
    {
        dump('UserObserver created a user.');

        dispatch(new SimpleJob())->afterCommit();
    }
}

Output:

Transaction started.
UserObserver created a user.
Sleeping 3 seconds.
Sleeping done.
Committing transaction.
CloudTasksApi::createTask(SimpleJob).
Done.

The CloudTasksApi::createTask is a dump that I temporarily added right before the task is created in Cloud Tasks.

Omg! You're right, the problem ocurred because I was not using afterCommit. And the error didn't help me to debug what was happening.

Maybe to help someone with the same error in the future, the error I was getting in this scenario is the following:

Google\ApiCore\ApiException: {
    "message": "Requested entity was not found.",
    "code": 5,
    "status": "NOT_FOUND",
    "details": []
}

Thanks for your help!