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