Skip to content

Commit

Permalink
Fixed stale rides in redis (#20)
Browse files Browse the repository at this point in the history
Fix rides stuck like this in redis:

<img width="834" alt="image"
src="https://github.com/better-rail/server/assets/12946462/0152a9d9-c98a-4207-89ad-e17ce77fca05">
  • Loading branch information
planecore authored Nov 3, 2023
1 parent 483fdeb commit 6e92830
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/data/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const addRide = async (ride: Ride): Promise<boolean> => {

export const updateLastRideNotification = async (rideId: string, notificationId: number) => {
try {
const isRideExists = await hasRide(rideId)
if (!isRideExists) {
return false
}

await client.hSet(getKey(rideId), "lastNotificationId", notificationId)

if (notificationId !== 0) {
Expand All @@ -52,10 +57,10 @@ export const updateRideToken = async (rideId: string, token: string) => {
try {
await client.hSet(getKey(rideId), "token", token)

logger.info(logNames.redis.rides.updateNotificationId.success, { rideId, token })
logger.info(logNames.redis.rides.updateToken.success, { rideId, token })
return true
} catch (error) {
logger.error(logNames.redis.rides.updateNotificationId.failed, { error, rideId, token })
logger.error(logNames.redis.rides.updateToken.failed, { error, rideId, token })
return false
}
}
Expand Down Expand Up @@ -93,7 +98,7 @@ export const deleteRide = async (rideId: string) => {
return success
} catch (error) {
try {
const isRideExists = await client.exists(getKey(rideId))
const isRideExists = await hasRide(rideId)
if (!isRideExists) {
return true
} else {
Expand All @@ -107,6 +112,15 @@ export const deleteRide = async (rideId: string) => {
}
}

export const hasRide = async (rideId: string) => {
try {
const result = await client.exists(getKey(rideId))
return Boolean(result)
} catch (error) {
return false
}
}

export const getAllRides = async (): Promise<Ride[] | null> => {
try {
const results = await client.keys("rides:*")
Expand Down

0 comments on commit 6e92830

Please sign in to comment.