Newer
Older
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { PrismaService } from '../prisma/prisma.service';
import { Cron } from '@nestjs/schedule';
import { PubSub } from 'graphql-subscriptions';
@Injectable()
export class TokenCodeScheduler {
constructor(
@Inject('PUBSUB')
private readonly pubSub: PubSub,
private readonly prisma: PrismaService,
) {}
private readonly incrementEvery = 1000 * 60 * 10;
private readonly values = [1, 2, 3, 5, 8];
@Cron('0 */1 * * * *')
async handleIncrement() {
const tasks = [];
for (let i = this.values.length - 2; i >= 0; i++) {
const task = this.prisma.tokenCode.updateMany({
where: {
value: this.values[i],
updatedAt: {
lt: new Date(Date.now() - this.incrementEvery),
},
},
data: {
value: this.values[i + 1],
},
});
tasks.push(task);
}
await this.prisma.$transaction(tasks);
// TODO: send agent updates over pubsub
}
}