У меня есть проект nodejs, использующий loopback и postgressql.
Я получил функцию handleTrade
, например, просто запустите транзакцию с тайм-аутом 30 с, запросите сделку по идентификатору и выполните некоторые обновления:
async function handleTrade(id) {
const { Trade } = app.models;
const transaction = await Trade.beginTransaction({
timeout: 30000 // 30 seconds
});
try {
console.log(`${id} - 1`);
const trade = await Trade.findById(id, { transaction });
console.log(`${id} - 2`);
if (trade) {
// Do some updates with the trade
}
transaction.commit();
} catch (e) {
console.log(`${id} - 3`, e);
transaction.rollback();
}
}
У меня есть список из 10 идентификаторов, я хочу обработать их все асинхронно и вывести Done
, когда все будет сделано.
async function test(ids) {
const promises = ids.map(id => handleTrade(id));
await Promise.all(promises);
console.log('Done');
process.exit(0);
}
test([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
А вот журнал:
2 - Step 1
3 - Step 1
1 - Step 1
4 - Step 1
6 - Step 1
5 - Step 1
10 - Step 1
9 - Step 1
8 - Step 1
7 - Step 1
-- The process stuck here for 30 seconds (which is the timeout value of the transaction)
2 - Step 2
Unhandled rejection Error: The transaction is not active: 914ca760-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
9 - Step 2
1 - Step 2
3 - Step 2
6 - Step 2
4 - Step 2
5 - Step 2
10 - Step 2
8 - Step 2
Unhandled rejection Error: The transaction is not active: 914e5510-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cce71-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cce70-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf581-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf580-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf582-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf583-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914e5511-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
7 - Step 2
Done
Процессы зависли на 30 секунд после шага 1, затем по истечении времени ожидания транзакции возвращается ошибка The transaction is not active
И это происходит только тогда, когда у меня есть 10 идентификаторов, если я использую только 9, он работает гладко
test([1, 2, 3, 4, 5, 6, 7, 8, 9]);
// Logs
2 - Step 1
4 - Step 1
5 - Step 1
9 - Step 1
7 - Step 1
8 - Step 1
6 - Step 1
1 - Step 1
3 - Step 1
2 - Step 2
4 - Step 2
9 - Step 2
8 - Step 2
1 - Step 2
5 - Step 2
7 - Step 2
6 - Step 2
3 - Step 2
Done
Если у вас есть идеи, пожалуйста, помогите мне. Спасибо