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