0
0

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,17 +5,23 @@ 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) => {
const promisedTo = User.findOne({ _id: data.toId }) as Promise<IRemoteUser>; try {
const [from, post] = await Promise.all([ const promisedTo = User.findOne({ _id: data.toId }) as Promise<IRemoteUser>;
User.findOne({ _id: data.fromId }), const [from, post] = await Promise.all([
Post.findOne({ _id: data.postId }) User.findOne({ _id: data.fromId }),
]); Post.findOne({ _id: data.postId })
const note = await renderNote(from, post); ]);
const to = await promisedTo; const note = await renderNote(from, post);
const create = renderCreate(note); const to = await promisedTo;
const create = renderCreate(note);
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,47 +21,46 @@ 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
} }
}), }),
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
} }
}), }),
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
}), }),
// Publish follow event // Publish follow event
isLocalUser(follower) && packUser(followee, follower) isLocalUser(follower) && packUser(followee, follower)
.then(packed => event(follower._id, 'follow', packed)), .then(packed => event(follower._id, 'follow', packed)),
isLocalUser(followee) && Promise.all([ isLocalUser(followee) && Promise.all([
packUser(follower, followee) packUser(follower, followee)
.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,35 +5,40 @@ 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) => {
const keyIdLower = data.signature.keyId.toLowerCase(); try {
let user; const keyIdLower = data.signature.keyId.toLowerCase();
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;
} else {
user = await User.findOne({
host: { $ne: null },
'account.publicKey.id': data.signature.keyId
}) as IRemoteUser;
if (user === null) {
user = await resolvePerson(data.signature.keyId);
}
} }
user = await User.findOne({ usernameLower: username, hostLower: host }) as IRemoteUser; if (user === null || !verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
} else { done();
user = await User.findOne({ return;
host: { $ne: null },
'account.publicKey.id': data.signature.keyId
}) as IRemoteUser;
if (user === null) {
user = await resolvePerson(data.signature.keyId);
} }
await Promise.all(await act(new Resolver(), user, data.inbox, true));
} catch (error) {
done(error);
return;
} }
if (user === null) { done();
throw 'failed to resolve user';
}
if (!verifySignature(data.signature, user.account.publicKey.publicKeyPem)) {
throw 'signature verification failed';
}
await Promise.all(await act(new Resolver(), user, data.inbox, true));
}; };

View File

@ -2,23 +2,30 @@ 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) => {
const asyncBot = User.findOne({ _id: data.userId }); try {
const asyncBot = User.findOne({ _id: data.userId });
// Fetch parent status // Fetch parent status
const parentStatuses = await request({ const parentStatuses = await request({
url: `${data.parentUrl}/statuses`, url: `${data.parentUrl}/statuses`,
headers: { headers: {
'User-Agent': 'misskey' 'User-Agent': 'misskey'
}, },
json: true json: true
}); });
const parentState = parentStatuses[0].state; const parentState = parentStatuses[0].state;
const stillFailed = parentState == 'failure' || parentState == 'error'; const stillFailed = parentState == 'failure' || parentState == 'error';
const text = stillFailed ? const text = stillFailed ?
`**⚠BUILD STILL FAILED⚠**: ?[${data.message}](${data.htmlUrl})` : `**⚠BUILD STILL FAILED⚠**: ?[${data.message}](${data.htmlUrl})` :
`**🚨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,24 +7,31 @@ 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;
User.findOne({ _id: following.followerId }),
User.findOne({ _id: following.followeeId })
]);
if (isLocalUser(follower) && isRemoteUser(followee)) { try {
const undo = renderUndo(renderFollow(follower, followee)); [follower, followee] = await Promise.all([
undo['@context'] = context; User.findOne({ _id: following.followerId }),
User.findOne({ _id: following.followeeId })
]);
await request(follower, followee.account.inbox, undo); if (isLocalUser(follower) && isRemoteUser(followee)) {
const undo = renderUndo(renderFollow(follower, followee));
undo['@context'] = context;
await request(follower, followee.account.inbox, undo);
}
} catch (error) {
done(error);
return;
} }
try { try {
@ -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();
} }
}; };