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

View file

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

View file

@ -14,4 +14,4 @@ const handlers = {
unfollow 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 act from '../../../remote/activitypub/act';
import Resolver from '../../../remote/activitypub/resolver'; 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(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 resolvePerson from '../../../remote/activitypub/resolve-person';
import Resolver from '../../../remote/activitypub/resolver'; import Resolver from '../../../remote/activitypub/resolver';
export default async ({ data }): Promise<void> => { export default async ({ data }, done) => {
try {
const keyIdLower = data.signature.keyId.toLowerCase(); const keyIdLower = data.signature.keyId.toLowerCase();
let user; let user;
if (keyIdLower.startsWith('acct:')) { if (keyIdLower.startsWith('acct:')) {
const { username, host } = parseAcct(keyIdLower.slice('acct:'.length)); const { username, host } = parseAcct(keyIdLower.slice('acct:'.length));
if (host === null) { if (host === null) {
throw 'request was made by local user'; done();
return;
} }
user = await User.findOne({ usernameLower: username, hostLower: host }) as IRemoteUser; user = await User.findOne({ usernameLower: username, hostLower: host }) as IRemoteUser;
@ -27,13 +29,16 @@ export default async ({ data }): Promise<void> => {
} }
} }
if (user === null) { if (user === null || !verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
throw 'failed to resolve user'; done();
} return;
if (!verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
throw 'signature verification failed';
} }
await Promise.all(await act(new Resolver(), user, data.inbox, true)); 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'; import User from '../../../models/user';
const createPost = require('../../../server/api/endpoints/posts/create'); 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 }); const asyncBot = User.findOne({ _id: data.userId });
// Fetch parent status // Fetch parent status
@ -21,4 +22,10 @@ export default async ({ data }) => {
`**🚨BUILD FAILED🚨**: →→→?[${data.message}](${data.htmlUrl})←←←`; `**🚨BUILD FAILED🚨**: →→→?[${data.message}](${data.htmlUrl})←←←`;
createPost({ text }, await asyncBot); 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 renderUndo from '../../../remote/activitypub/renderer/undo';
import context from '../../../remote/activitypub/renderer/context'; import context from '../../../remote/activitypub/renderer/context';
import request from '../../../remote/request'; 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 }); const following = await Following.findOne({ _id: data.id });
if (following === null) { if (following === null) {
done();
return; 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.followerId }),
User.findOne({ _id: following.followeeId }) User.findOne({ _id: following.followeeId })
]); ]);
@ -26,6 +29,10 @@ export default async ({ data }) => {
await request(follower, followee.account.inbox, undo); await request(follower, followee.account.inbox, undo);
} }
} catch (error) {
done(error);
return;
}
try { try {
await Promise.all([ await Promise.all([
@ -57,7 +64,7 @@ export default async ({ data }) => {
// Publish follow event // Publish follow event
stream(follower._id, 'unfollow', promisedPackedUser); stream(follower._id, 'unfollow', promisedPackedUser);
} catch (error) { } finally {
Logger.error(error.toString()); done();
} }
}; };