78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import { URL } from 'node:url';
|
|
import Bull from 'bull';
|
|
import { request } from '@/remote/activitypub/request.js';
|
|
import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc.js';
|
|
import { Instances } from '@/models/index.js';
|
|
import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js';
|
|
import { toPuny } from '@/misc/convert-host.js';
|
|
import { StatusError } from '@/misc/fetch.js';
|
|
import { getUserKeypair } from '@/misc/keypair-store.js';
|
|
import { shouldSkipInstance } from '@/misc/skipped-instances.js';
|
|
import { DeliverJobData } from '@/queue/types.js';
|
|
|
|
export default async (job: Bull.Job<DeliverJobData>) => {
|
|
const { host } = new URL(job.data.to);
|
|
const puny = toPuny(host);
|
|
|
|
// for the first few tries (where most attempts will be made)
|
|
// we assume that inserting deliver jobs took care of this check
|
|
// only on later attempts do we actually do it, to ease database
|
|
// performance. this might cause a slight delay of a few minutes
|
|
// for instance blocks being applied
|
|
//
|
|
// with apBackoff, attempt 2 happens ~4min after the initial try, while
|
|
// attempt 3 happens ~11 min after the initial try, which seems like a
|
|
// good tradeoff between database and blocks being applied reasonably quick
|
|
if (job.attemptsMade >= 3 && await shouldSkipInstance(puny)) {
|
|
return 'skip';
|
|
}
|
|
|
|
const keypair = await getUserKeypair(job.data.user.id);
|
|
|
|
try {
|
|
if (Array.isArray(job.data.content)) {
|
|
await Promise.all(
|
|
job.data.content.map(x => request(job.data.user, job.data.to, x, keypair))
|
|
);
|
|
} else {
|
|
await request(job.data.user, job.data.to, job.data.content, keypair);
|
|
}
|
|
|
|
// Update stats
|
|
registerOrFetchInstanceDoc(host).then(i => {
|
|
Instances.update(i.id, {
|
|
latestRequestSentAt: new Date(),
|
|
latestStatus: 200,
|
|
lastCommunicatedAt: new Date(),
|
|
isNotResponding: false,
|
|
});
|
|
|
|
fetchInstanceMetadata(i);
|
|
});
|
|
} catch (res) {
|
|
// Update stats
|
|
registerOrFetchInstanceDoc(host).then(i => {
|
|
Instances.update(i.id, {
|
|
latestRequestSentAt: new Date(),
|
|
latestStatus: res instanceof StatusError ? res.statusCode : null,
|
|
isNotResponding: true,
|
|
});
|
|
});
|
|
|
|
if (res instanceof StatusError) {
|
|
// 4xx
|
|
if (res.isClientError) {
|
|
// A client error means that something is wrong with the request we are making,
|
|
// which means that retrying it makes no sense.
|
|
return `${res.statusCode} ${res.statusMessage}`;
|
|
}
|
|
|
|
// 5xx etc., throwing an Error will make Bull retry
|
|
throw new Error(`${res.statusCode} ${res.statusMessage}`);
|
|
} else {
|
|
// DNS error, socket error, timeout ...
|
|
throw res;
|
|
}
|
|
}
|
|
};
|