server: rewrite skipped instances query in raw SQL

This commit is contained in:
Johann150 2022-11-10 00:38:11 +01:00
parent 5d23aa9e69
commit 1c4967015c
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,4 +1,5 @@
import { Brackets } from 'typeorm'; import { Brackets } from 'typeorm';
import { db } from '@/db/postgre.js';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import { Instances } from '@/models/index.js'; import { Instances } from '@/models/index.js';
import { Instance } from '@/models/entities/instance.js'; import { Instance } from '@/models/entities/instance.js';
@ -25,19 +26,16 @@ export async function skippedInstances(hosts: Array<Instace['host']>): Array<Ins
const deadTime = new Date(Date.now() - deadThreshold); const deadTime = new Date(Date.now() - deadThreshold);
return skipped.concat( return skipped.concat(
await Instances.createQueryBuilder('instance') await db.query(
.where('instance.host in (:...hosts)', { 'SELECT host FROM instance WHERE ("isSuspended" OR "latestStatus" = 410 OR "lastCommunicatedAt" < $1::date) AND host = ANY(string_to_array($2, \',\'))',
[
deadTime.toISOString(),
// don't check hosts again that we already know are suspended // don't check hosts again that we already know are suspended
// also avoids adding duplicates to the list // also avoids adding duplicates to the list
hosts: hosts.filter(host => !skipped.includes(host)), hosts.filter(host => !skipped.includes(host) && !host.includes(',')).join(','),
}) ],
.andWhere(new Brackets(qb => { qb )
.where('instance.isSuspended') .then(res => res.map(row => row.host))
.orWhere('instance.lastCommunicatedAt < :deadTime', { deadTime })
.orWhere('instance.latestStatus = 410');
}))
.select('host')
.getRawMany()
); );
} }