-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: queues status endpoint to handle jobs states correctly (#24)
- Loading branch information
Showing
15 changed files
with
358 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { AppComponents, EntityStatusInQueue, QueuesStatusManagerComponent } from '../types' | ||
|
||
export function createQueuesStatusManagerComponent({ | ||
memoryStorage | ||
}: Pick<AppComponents, 'memoryStorage'>): QueuesStatusManagerComponent { | ||
function generateCacheKey(platform: 'windows' | 'mac' | 'webgl', entityId: string): string { | ||
return `jobs:${platform}:${entityId}` | ||
} | ||
|
||
async function getStatus(key: string): Promise<EntityStatusInQueue | undefined> { | ||
const status = await memoryStorage.get<EntityStatusInQueue>(key) | ||
|
||
if (!status.length) return undefined | ||
|
||
return status[0] | ||
} | ||
|
||
async function markAsQueued(platform: 'windows' | 'mac' | 'webgl', entityId: string): Promise<void> { | ||
const key = generateCacheKey(platform, entityId) | ||
const currentValue = (await getStatus(key)) || { | ||
entityId, | ||
platform, | ||
status: 0 | ||
} | ||
|
||
await memoryStorage.set(key, { ...currentValue, status: currentValue.status + 1 }) | ||
} | ||
|
||
async function markAsFinished(platform: 'windows' | 'mac' | 'webgl', entityId: string): Promise<void> { | ||
const key = generateCacheKey(platform, entityId) | ||
const currentValue = (await getStatus(key)) || { | ||
entityId, | ||
platform, | ||
status: 0 | ||
} | ||
|
||
const newStatus = currentValue.status - 1 | ||
|
||
if (newStatus === 0) { | ||
await memoryStorage.purge(key) | ||
return | ||
} | ||
|
||
await memoryStorage.set<EntityStatusInQueue>(key, { ...currentValue, status: newStatus }) | ||
} | ||
|
||
async function getAllPendingEntities(platform: 'windows' | 'mac' | 'webgl'): Promise<EntityStatusInQueue[]> { | ||
const entities = (await memoryStorage.get<EntityStatusInQueue>(`jobs:${platform}:*`)) || [] | ||
return entities | ||
.filter((entity: any) => entity.status > 0) | ||
.map((entity) => ({ | ||
...entity, | ||
status: Math.min(entity.status, 1) | ||
})) | ||
} | ||
|
||
return { | ||
markAsQueued, | ||
markAsFinished, | ||
getAllPendingEntities | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.