🐛 Trying to fix check in locking

This commit is contained in:
LittleSheep 2025-05-25 12:26:12 +08:00
parent 68399dd371
commit c562f52538
2 changed files with 17 additions and 9 deletions

View File

@ -362,6 +362,8 @@ public class AccountController(
if (!isAvailable)
return BadRequest("Check-in is not available for today.");
try
{
var needsCaptcha = await events.CheckInDailyDoAskCaptcha(currentUser);
return needsCaptcha switch
{
@ -371,6 +373,11 @@ public class AccountController(
_ => await events.CheckInDaily(currentUser)
};
}
catch (InvalidOperationException ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("me/calendar")]
[Authorize]

View File

@ -142,7 +142,8 @@ public class AccountEventService(
public async Task<CheckInResult> CheckInDaily(Account user)
{
var lockKey = $"{CheckInLockKey}{user.Id}";
var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(10), TimeSpan.Zero);
await using var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(10), TimeSpan.Zero);
if (lk is null) throw new InvalidOperationException("Check-in was in progress.");
var cultureInfo = new CultureInfo(user.Language, false);
@ -212,7 +213,7 @@ public class AccountEventService(
ActivityVisibility.Friends
);
await lk.ReleaseAsync();
// The lock will be automatically released by the await using statement
return result;
}