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

Wallet balance not updated inside job when run multiple times #1021

Open
mostafahosny138 opened this issue Feb 11, 2025 · 3 comments
Open

Wallet balance not updated inside job when run multiple times #1021

mostafahosny138 opened this issue Feb 11, 2025 · 3 comments
Assignees
Labels
question Further information is requested Stale

Comments

@mostafahosny138
Copy link

mostafahosny138 commented Feb 11, 2025

I am experiencing an issue where the wallet balance is not updated correctly inside a job when the job is run multiple times. Here are the steps to reproduce the issue:

1-Job Dispatch:
I dispatch a job to process a group of users:

dispatch(new TransferInheritanceJob())->onQueue('inheritance');

2-Job Logic:
Inside the job, I loop through a group of users and perform the following operations for each user:

Get the user's wallet:
$wallet = $user->getWallet('seller');

Check the wallet balance. If the balance is less than or equal to zero, return an error message and skip the user
if ($wallet->balance <= 0) {
return 'inherited wallet balance less/equal zero inherited_id';
continue;
}

Insert a withdrawal record into the transactions table and refresh the wallet:
DB::table('transactions')->insertGetId($data);
$wallet->refreshBalance();

3-Expected Behavior:
After the first job run, the wallet balance should be updated to 0.
When the job runs again for the same user, the $wallet->balance should reflect the updated balance (0).

4-Actual Behavior:
When the job runs for the second time, $wallet->balance still returns the balance before the withdrawal operation.
The balance is only updated correctly after restarting the worker.
The database is updated correctly (the balance is 0 in the database), but the job does not reflect this change.

5-Additional Observations:
The balance is correct when checked in the user profile after the first job run.
The issue only occurs inside the job when it is run multiple times.

6-Environment:
PHP version: 8.1.0
Database: MySQL
Laravel Wallet version: 10.1.0
Cache lock: Redis
Cache wallets: Redis

Could you please help me understand what the problem might be? Is this a known issue, or am I missing something in my implementation? If there’s anything unclear in my description, please let me know, and I’ll be happy to provide more details or clarify furthe

@mostafahosny138 mostafahosny138 added bug Something isn't working question Further information is requested labels Feb 11, 2025
@rez1dent3 rez1dent3 removed the bug Something isn't working label Feb 11, 2025
@rez1dent3
Copy link
Member

Hello. Direct data insertion is not allowed. You need to work with wallet methods and put locks, otherwise you will get a race condition, which is what you are experiencing.

Your problem is exactly in this code:

if ($wallet->balance <= 0) {
    return 'inherited wallet balance less/equal zero inherited_id';
}

DB::table('transactions')->insertGetId($data);
$wallet->refreshBalance();

Firstly, you do not block work with the wallet during the verification, which means that the user can continue working with the wallet after checking your condition.

Secondly, I see continue, most likely, you are working inside a cycle (I will not focus on return). Take a closer look at the fast API methods: https://bavix.github.io/laravel-wallet/guide/high-performance/batch-transactions.html

@mostafahosny138
Copy link
Author

Thanks alot
now I am working on it and apply your advices and when i finish i will get feedback

thanks

Copy link

This issue is stale because it has been open 7 days with no activity.

@github-actions github-actions bot added the Stale label Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

2 participants