Improvoe focolive, add job views
This commit is contained in:
@@ -4,6 +4,22 @@ import Redis from 'ioredis'
|
||||
import { InjectRedis } from '@liaoliaots/nestjs-redis';
|
||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
import { Cache } from 'cache-manager';
|
||||
import { aperture } from 'ramda';
|
||||
|
||||
export interface JobMetadata {
|
||||
name: string;
|
||||
description: string;
|
||||
tags: string[];
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
claimSecret: string;
|
||||
}
|
||||
|
||||
export const privateMetadataKeys = ['claimSecret'];
|
||||
|
||||
export type PublicJobMetadata = Omit<JobMetadata, typeof privateMetadataKeys[number]>;
|
||||
|
||||
type Leaderboard = { completeCounts: { [claimer: string]: number }, claimCounts: { [claimer: string]: number } }
|
||||
|
||||
@Injectable()
|
||||
export class JobsService {
|
||||
@@ -13,6 +29,10 @@ export class JobsService {
|
||||
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
|
||||
) { }
|
||||
|
||||
public static cleanJobMetadata(metadata: JobMetadata): PublicJobMetadata {
|
||||
return Object.fromEntries(Object.entries(metadata).filter(([key]) => !privateMetadataKeys.includes(key))) as PublicJobMetadata;
|
||||
}
|
||||
|
||||
private jobNameBuilder(jobName: string) {
|
||||
return `job:${jobName}`;
|
||||
}
|
||||
@@ -37,7 +57,11 @@ export class JobsService {
|
||||
return `claim:${jobName}:${claimer}`;
|
||||
}
|
||||
|
||||
private async getCompleteCounts(jobName: string) {
|
||||
private claimerCountWildcardBuilder(jobName: string) {
|
||||
return `claim:${jobName}:*`;
|
||||
}
|
||||
|
||||
private async getCompleteCounts(jobName: string): Promise<{ [claimer: string]: number }> {
|
||||
const keys = await this.redis.keys(`complete:${jobName}:*`);
|
||||
const counts = await Promise.all(keys.map(async key => {
|
||||
const count = await this.redis.get(key);
|
||||
@@ -55,7 +79,7 @@ export class JobsService {
|
||||
}, {})
|
||||
}
|
||||
|
||||
private async getClaimCounts(jobName: string) {
|
||||
private async getClaimCounts(jobName: string): Promise<{ [claimer: string]: number }> {
|
||||
const keys = await this.redis.keys(`claim:${jobName}:*`);
|
||||
const counts = await Promise.all(keys.map(async key => {
|
||||
const count = await this.redis.get(key);
|
||||
@@ -73,8 +97,8 @@ export class JobsService {
|
||||
}, {});
|
||||
}
|
||||
|
||||
async getLeaderboard(jobName: string) {
|
||||
const cachedLeaderboard = await this.cacheManager.get(`leaderboard:${jobName}`);
|
||||
async getLeaderboard(jobName: string): Promise<Leaderboard> {
|
||||
const cachedLeaderboard = await this.cacheManager.get<Leaderboard>(`leaderboard:${jobName}`);
|
||||
if (cachedLeaderboard) {
|
||||
return cachedLeaderboard;
|
||||
}
|
||||
@@ -86,24 +110,30 @@ export class JobsService {
|
||||
|
||||
|
||||
async addItemsToJob(jobName: string, items: string[]) {
|
||||
await this.redis.rpush(this.todoListNameBuilder(jobName), ...items);
|
||||
const apertureSize = 100
|
||||
for (const itemSubset of (items.length > apertureSize ? aperture(apertureSize, items) : [items])) {
|
||||
await this.redis.rpush(this.todoListNameBuilder(jobName), ...itemSubset);
|
||||
}
|
||||
}
|
||||
|
||||
async claimJobItem(jobName: string, claimer: string): Promise<string | null> {
|
||||
const jobItem = await this.redis.brpoplpush(this.todoListNameBuilder(jobName), this.claimedListNameBuilder(jobName), 10);
|
||||
if (jobItem) {
|
||||
await this.redis.rpush(this.jobNameBuilder(jobName), JSON.stringify({ item: jobItem, client: claimer }));
|
||||
if (!jobItem) {
|
||||
return null;
|
||||
}
|
||||
await this.redis.incr(this.claimerCountNameBuilder(jobName, claimer));
|
||||
return jobItem;
|
||||
}
|
||||
|
||||
async completeJobItem(jobName: string, jobItem: string, completer: string, data: any) {
|
||||
await this.redis.lrem(this.claimedListNameBuilder(jobName), 1, jobItem);
|
||||
await this.redis.lrem(this.todoListNameBuilder(jobName), 1, JSON.stringify({ item: jobItem, client: completer }));
|
||||
const claimRemoveResult = await this.redis.lrem(this.claimedListNameBuilder(jobName), 1, jobItem);
|
||||
if (claimRemoveResult === 0) {
|
||||
return false;
|
||||
}
|
||||
await this.redis.rpush(this.doneListNameBuilder(jobName), JSON.stringify({ item: jobItem, client: completer, data }));
|
||||
await this.redis.decr(this.claimerCountNameBuilder(jobName, completer));
|
||||
await this.redis.incr(this.completeCountNameBuilder(jobName, completer));
|
||||
return true
|
||||
}
|
||||
|
||||
async getTodoItems(jobName: string) {
|
||||
@@ -134,11 +164,19 @@ export class JobsService {
|
||||
return this.redis.keys('job:*');
|
||||
}
|
||||
|
||||
async registerJob(jobName: string, metadata: any) {
|
||||
await this.redis.set(this.jobNameBuilder(jobName), JSON.stringify(metadata));
|
||||
async isJobRegistered(jobName: string) {
|
||||
return this.redis.exists(this.jobNameBuilder(jobName));
|
||||
}
|
||||
|
||||
async getJobMetadata(jobName: string): Promise<any | null> {
|
||||
async registerJob(jobName: string, metadata: JobMetadata) {
|
||||
if (await this.isJobRegistered(jobName)) {
|
||||
return false;
|
||||
}
|
||||
await this.redis.set(this.jobNameBuilder(jobName), JSON.stringify(metadata));
|
||||
return true
|
||||
}
|
||||
|
||||
async getJobMetadata(jobName: string): Promise<JobMetadata | null> {
|
||||
const result = await this.redis.get(this.jobNameBuilder(jobName))
|
||||
if (!result) {
|
||||
return null;
|
||||
@@ -146,11 +184,34 @@ export class JobsService {
|
||||
return JSON.parse(result)
|
||||
}
|
||||
|
||||
async getPublicJobMetadata(jobName: string): Promise<PublicJobMetadata | null> {
|
||||
const metadata = await this.getJobMetadata(jobName);
|
||||
if (!metadata) {
|
||||
return null;
|
||||
}
|
||||
return JobsService.cleanJobMetadata(metadata);
|
||||
}
|
||||
|
||||
async clearClaimerCounts(jobName: string) {
|
||||
const keys = await this.redis.keys(this.claimerCountWildcardBuilder(jobName));
|
||||
await Promise.all(keys.map(key => this.redis.del(key)));
|
||||
}
|
||||
|
||||
async resetClaimedItems(jobName: string) {
|
||||
const claimedItems = await this.getClaimedItems(jobName);
|
||||
for (const claimedItem of claimedItems) {
|
||||
await this.redis.rpoplpush(this.claimedListNameBuilder(jobName), this.todoListNameBuilder(jobName));
|
||||
}
|
||||
await this.clearClaimerCounts(jobName);
|
||||
}
|
||||
|
||||
async clearTodoItems(jobName: string, claimKey: string) {
|
||||
const metadata = await this.getJobMetadata(jobName);
|
||||
if (metadata === null || metadata.claimSecret !== claimKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.redis.del(this.todoListNameBuilder(jobName));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user