Skip to content

Commit

Permalink
fix(#120): fixed a bug with mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Jun 5, 2023
1 parent c649f0b commit c3d3da9
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class AutoRaffleActor(private val raffleActor: RaffleActor) : CoroutineScope {
.forEach { schedule(bot, it, di) }

suspend fun cancelSchedulesRaffle(giveawayId: Long): Boolean {
println("[$giveawayId]: CANCELLING RAFFLE")
return scheduledMutex.withLock {
scheduled[giveawayId]?.cancel()
println("[$giveawayId]: GOT MUTEX")
scheduled[giveawayId]?.cancelAndJoin()
println("[$giveawayId]: CANCELLED")
scheduled.remove(giveawayId) != null
}
}
Expand All @@ -53,12 +56,13 @@ class AutoRaffleActor(private val raffleActor: RaffleActor) : CoroutineScope {
val delayMillis = giveaway.raffleDate.toInstant().toEpochMilli() - Instant.now().toEpochMilli()
delay(delayMillis)

scheduledMutex.withLock {
if (giveaway.id in scheduled && di.getGiveawayById(giveaway.id) != null) {
scheduled.remove(giveaway.id)
handleRaffleResult(bot, di, giveaway, raffleActor.raffle(bot, giveaway, di))
}
scheduledMutex.lock()
if (giveaway.id in scheduled && di.getGiveawayById(giveaway.id) != null) {
scheduled.remove(giveaway.id)
scheduledMutex.unlock()
handleRaffleResult(bot, di, giveaway, raffleActor.raffle(bot, giveaway, di))
}
if (scheduledMutex.isLocked) scheduledMutex.unlock()
}
}
}
Expand Down

0 comments on commit c3d3da9

Please sign in to comment.