Skip to content

Commit

Permalink
[PLA-1463] Add atomic locking (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Nov 29, 2023
1 parent 99ac3b0 commit 2f24cc7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr_agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- name: PR Agent action step
id: pragent
uses: Codium-ai/pr-agent@main
uses: Codium-ai/pr-agent@v0.10
env:
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions src/Enums/PlatformBeamCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum PlatformBeamCache: string implements PlatformCacheable
case CLAIM_PROBABILITIES = 'claimProbabilities';
case IDEMPOTENCY_KEY = 'idempotencyKey';
case BATCH_PROCESS = 'batchProcessKey';
case CLAIM_BEAM_JOB = 'claimBeamJob';

/**
* The key for the cache item.
Expand Down
15 changes: 12 additions & 3 deletions src/Jobs/ClaimBeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Services\Database\WalletService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Cache\LockTimeoutException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
Expand Down Expand Up @@ -41,7 +42,10 @@ public function __construct(protected ?array $data)
public function handle(BatchService $batch, WalletService $wallet): void
{
if ($data = $this->data) {
$lock = Cache::lock(PlatformBeamCache::CLAIM_BEAM_JOB->key($data['beam']['id']), 5);

try {
$lock->block(5);
$claim = BeamClaim::where('beam_id', $data['beam']['id'])
->claimable()
->when($data['code'], fn ($query) => $query->withSingleUseCode($data['code']))
Expand All @@ -58,17 +62,22 @@ public function handle(BatchService $batch, WalletService $wallet): void

DB::commit();

Log::info('Claim beam assigned.', $data);
Log::info('ClaimBeamJob: Claim assigned.', $claim->toArray());
} else {
Log::info('Claim beam cannot assign.', $data);
Log::info('ClaimBeamJob: No claim assigned.', $data);
$this->release(1);
}
} catch(LockTimeoutException) {
Log::info('ClaimBeamJob: Cannot obtain lock, retrying', $data);
$this->release(1);
} catch (Throwable $e) {
DB::rollBack();

Log::error('Claim beam error, message:' . $e->getMessage(), $data);
Log::error('ClaimBeamJob: Claim error, message:' . $e->getMessage(), $data);

throw $e;
} finally {
$lock?->release();
}
}
}
Expand Down

0 comments on commit 2f24cc7

Please sign in to comment.