【发布时间】:2021-02-19 01:26:33
【问题描述】:
所以我有一个服务器正在监听 RabbitMQ 请求:
console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', q);
channel.consume(q, async function reply(msg) {
const mongodbUserId = msg.content.toString();
console.log(' [x] Received %s', mongodbUserId);
await exec('./new_user_run_athena.sh ' + mongodbUserId, function(
error,
stdout,
stderr
) {
console.log('Running Athena...');
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
console.log(
' Finished running Athena for mongodbUserId=%s',
mongodbUserId
);
channel.sendToQueue(
msg.properties.replyTo,
new Buffer(mongodbUserId),
{ correlationId: msg.properties.correlationId }
);
channel.ack(msg);
});
问题是在我打印出Finished running Athena for mongodbUserId 之后,执行shell 脚本new_user_run_athena.sh 的等待调用发生了。您可以在控制台日志中看到它发生的情况:
[*] Waiting for messages in run_athena_for_new_user_queue. To exit press CTRL+C
[x] Received 5aa96f36ed4f68154f3f2143
Finished running Athena for mongodbUserId=5aa96f36ed4f68154f3f2143
Running Athena...
stdout:
stderr:
甚至可以在执行 shell 脚本时使用 async await 语法吗?
【问题讨论】:
-
您的代码中的
exec是什么?child_process.exec?因为它不返回承诺。所以在它上面使用await没有任何作用。
标签: javascript shell async-await