Let unhandled rejection handler handle rejections in jobs

This commit is contained in:
Akihiko Odaki 2018-04-05 01:04:44 +09:00
parent d7c13b975f
commit e330ac1934
9 changed files with 134 additions and 111 deletions

View File

@ -5,7 +5,7 @@ import PostReaction from '../../../models/post-reaction';
import PostWatching from '../../../models/post-watching';
import Post from '../../../models/post';
export default async ({ data }) => Promise.all([
export default ({ data }, done) => Promise.all([
Favorite.remove({ postId: data._id }),
Notification.remove({ postId: data._id }),
PollVote.remove({ postId: data._id }),
@ -19,4 +19,4 @@ export default async ({ data }) => Promise.all([
}),
Post.remove({ repostId: data._id })
]))
]);
]).then(() => done(), done);

View File

@ -4,4 +4,4 @@ const handlers = {
deletePostDependents
};
export default (job, done) => handlers[job.data.type](job).then(() => done(), done);
export default (job, done) => handlers[job.data.type](job, done);

View File

@ -5,7 +5,8 @@ import renderCreate from '../../../remote/activitypub/renderer/create';
import renderNote from '../../../remote/activitypub/renderer/note';
import request from '../../../remote/request';
export default async ({ data }) => {
export default async ({ data }, done) => {
try {
const promisedTo = User.findOne({ _id: data.toId }) as Promise<IRemoteUser>;
const [from, post] = await Promise.all([
User.findOne({ _id: data.fromId }),
@ -17,5 +18,10 @@ export default async ({ data }) => {
create['@context'] = context;
return request(from, to.account.inbox, create);
await request(from, to.account.inbox, create);
} catch (error) {
done(error);
}
done();
};

View File

@ -7,10 +7,8 @@ import notify from '../../../publishers/notify';
import context from '../../../remote/activitypub/renderer/context';
import render from '../../../remote/activitypub/renderer/follow';
import request from '../../../remote/request';
import Logger from '../../../utils/logger';
export default async ({ data }) => {
const { followerId, followeeId } = await Following.findOne({ _id: data.following });
export default ({ data }, done) => Following.findOne({ _id: data.following }).then(async ({ followerId, followeeId }) => {
const [follower, followee] = await Promise.all([
User.findOne({ _id: followerId }),
User.findOne({ _id: followeeId })
@ -23,10 +21,10 @@ export default async ({ data }) => {
await request(follower, followee.account.inbox, rendered);
}
try {
await Promise.all([
return [follower, followee];
}).then(([follower, followee]) => Promise.all([
// Increment following count
User.update(followerId, {
User.update(follower._id, {
$inc: {
followingCount: 1
}
@ -34,12 +32,12 @@ export default async ({ data }) => {
FollowingLog.insert({
createdAt: data.following.createdAt,
userId: followerId,
userId: follower._id,
count: follower.followingCount + 1
}),
// Increment followers count
User.update({ _id: followeeId }, {
User.update({ _id: followee._id }, {
$inc: {
followersCount: 1
}
@ -47,7 +45,7 @@ export default async ({ data }) => {
FollowedLog.insert({
createdAt: data.following.createdAt,
userId: followerId,
userId: follower._id,
count: followee.followersCount + 1
}),
@ -60,10 +58,9 @@ export default async ({ data }) => {
.then(packed => event(followee._id, 'followed', packed)),
// Notify
isLocalUser(followee) && notify(followeeId, followerId, 'follow')
isLocalUser(followee) && notify(followee._id, follower._id, 'follow')
])
]);
} catch (error) {
Logger.error(error.toString());
}
};
]).then(() => done(), error => {
done();
throw error;
}), done);

View File

@ -14,4 +14,4 @@ const handlers = {
unfollow
};
export default (job, done) => handlers[job.data.type](job).then(() => done(), done);
export default (job, done) => handlers[job.data.type](job, done);

View File

@ -2,6 +2,7 @@ import User from '../../../models/user';
import act from '../../../remote/activitypub/act';
import Resolver from '../../../remote/activitypub/resolver';
export default ({ data }) => User.findOne({ _id: data.actor })
export default ({ data }, done) => User.findOne({ _id: data.actor })
.then(actor => act(new Resolver(), actor, data.outbox))
.then(Promise.all);
.then(Promise.all)
.then(() => done(), done);

View File

@ -5,14 +5,16 @@ import act from '../../../remote/activitypub/act';
import resolvePerson from '../../../remote/activitypub/resolve-person';
import Resolver from '../../../remote/activitypub/resolver';
export default async ({ data }): Promise<void> => {
export default async ({ data }, done) => {
try {
const keyIdLower = data.signature.keyId.toLowerCase();
let user;
if (keyIdLower.startsWith('acct:')) {
const { username, host } = parseAcct(keyIdLower.slice('acct:'.length));
if (host === null) {
throw 'request was made by local user';
done();
return;
}
user = await User.findOne({ usernameLower: username, hostLower: host }) as IRemoteUser;
@ -27,13 +29,16 @@ export default async ({ data }): Promise<void> => {
}
}
if (user === null) {
throw 'failed to resolve user';
}
if (!verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
throw 'signature verification failed';
if (user === null || !verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
done();
return;
}
await Promise.all(await act(new Resolver(), user, data.inbox, true));
} catch (error) {
done(error);
return;
}
done();
};

View File

@ -2,7 +2,8 @@ import * as request from 'request-promise-native';
import User from '../../../models/user';
const createPost = require('../../../server/api/endpoints/posts/create');
export default async ({ data }) => {
export default async ({ data }, done) => {
try {
const asyncBot = User.findOne({ _id: data.userId });
// Fetch parent status
@ -21,4 +22,10 @@ export default async ({ data }) => {
`**🚨BUILD FAILED🚨**: →→→?[${data.message}](${data.htmlUrl})←←←`;
createPost({ text }, await asyncBot);
} catch (error) {
done(error);
return;
}
done();
};

View File

@ -7,15 +7,18 @@ import renderFollow from '../../../remote/activitypub/renderer/follow';
import renderUndo from '../../../remote/activitypub/renderer/undo';
import context from '../../../remote/activitypub/renderer/context';
import request from '../../../remote/request';
import Logger from '../../../utils/logger';
export default async ({ data }) => {
export default async ({ data }, done) => {
const following = await Following.findOne({ _id: data.id });
if (following === null) {
done();
return;
}
const [follower, followee] = await Promise.all([
let follower, followee;
try {
[follower, followee] = await Promise.all([
User.findOne({ _id: following.followerId }),
User.findOne({ _id: following.followeeId })
]);
@ -26,6 +29,10 @@ export default async ({ data }) => {
await request(follower, followee.account.inbox, undo);
}
} catch (error) {
done(error);
return;
}
try {
await Promise.all([
@ -57,7 +64,7 @@ export default async ({ data }) => {
// Publish follow event
stream(follower._id, 'unfollow', promisedPackedUser);
} catch (error) {
Logger.error(error.toString());
} finally {
done();
}
};