Sharkey/src/queue/processors/http/deliver.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-07-26 02:11:47 +03:00
import * as bq from 'bee-queue';
2018-04-04 17:12:35 +03:00
2018-04-09 20:12:17 +03:00
import request from '../../../remote/activitypub/request';
import { queueLogger } from '../../logger';
2019-02-07 08:00:44 +02:00
import { registerOrFetchInstanceDoc } from '../../../services/register-or-fetch-instance-doc';
import Instance from '../../../models/instance';
import instanceChart from '../../../services/chart/instance';
2018-04-04 17:12:35 +03:00
2018-07-26 02:11:47 +03:00
export default async (job: bq.Job, done: any): Promise<void> => {
2019-02-07 16:37:39 +02:00
const { host } = new URL(job.data.to);
2018-04-05 12:43:06 +03:00
try {
await request(job.data.user, job.data.to, job.data.content);
2019-02-07 08:00:44 +02:00
// Update stats
2019-02-07 16:37:39 +02:00
registerOrFetchInstanceDoc(host).then(i => {
2019-02-07 08:00:44 +02:00
Instance.update({ _id: i._id }, {
$set: {
latestRequestSentAt: new Date(),
latestStatus: 200
}
});
instanceChart.requestSent(i.host, true);
2019-02-07 08:00:44 +02:00
});
2018-04-05 12:43:06 +03:00
done();
2018-04-07 00:22:03 +03:00
} catch (res) {
2019-02-07 08:00:44 +02:00
// Update stats
2019-02-07 16:37:39 +02:00
registerOrFetchInstanceDoc(host).then(i => {
2019-02-07 08:00:44 +02:00
Instance.update({ _id: i._id }, {
$set: {
latestRequestSentAt: new Date(),
latestStatus: res != null && res.hasOwnProperty('statusCode') ? res.statusCode : null
}
});
instanceChart.requestSent(i.host, false);
2019-02-07 08:00:44 +02:00
});
if (res != null && res.hasOwnProperty('statusCode')) {
2019-02-07 08:00:44 +02:00
queueLogger.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`);
if (res.statusCode >= 400 && res.statusCode < 500) {
// HTTPステータスコード4xxはクライアントエラーであり、それはつまり
// 何回再送しても成功することはないということなのでエラーにはしないでおく
done();
} else {
done(res.statusMessage);
}
2018-04-07 00:22:03 +03:00
} else {
queueLogger.warn(`deliver failed: ${res} to=${job.data.to}`);
done();
2018-04-07 00:22:03 +03:00
}
2018-04-05 12:43:06 +03:00
}
2018-04-04 17:22:48 +03:00
};