diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 13:26:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 14:14:16 +0200 |
commit | 6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch) | |
tree | 3365a96d82bc7f00ae504a568725c8e914150cf8 /server/helpers/database-utils.ts | |
parent | 5fe7e898316e18369c3e1aba307b55077adc7bfb (diff) | |
download | PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip |
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r-- | server/helpers/database-utils.ts | 69 |
1 files changed, 22 insertions, 47 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index f8ee9a454..f761cfb89 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts | |||
@@ -1,70 +1,45 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | // TODO: import from ES6 when retry typing file will include errorFilter function | 1 | // TODO: import from ES6 when retry typing file will include errorFilter function |
3 | import * as retry from 'async/retry' | 2 | import * as retry from 'async/retry' |
3 | import * as Promise from 'bluebird' | ||
4 | 4 | ||
5 | import { database as db } from '../initializers/database' | ||
6 | import { logger } from './logger' | 5 | import { logger } from './logger' |
7 | 6 | ||
8 | function commitTransaction (t: Sequelize.Transaction, callback: (err: Error) => void) { | ||
9 | return t.commit().asCallback(callback) | ||
10 | } | ||
11 | |||
12 | function rollbackTransaction (err: Error, t: Sequelize.Transaction, callback: (err: Error) => void) { | ||
13 | // Try to rollback transaction | ||
14 | if (t) { | ||
15 | // Do not catch err, report the original one | ||
16 | t.rollback().asCallback(function () { | ||
17 | return callback(err) | ||
18 | }) | ||
19 | } else { | ||
20 | return callback(err) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] } | 7 | type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] } |
25 | function retryTransactionWrapper (functionToRetry: Function, options: RetryTransactionWrapperOptions, finalCallback: Function) { | 8 | function retryTransactionWrapper (functionToRetry: (... args) => Promise<any>, options: RetryTransactionWrapperOptions) { |
26 | const args = options.arguments ? options.arguments : [] | 9 | const args = options.arguments ? options.arguments : [] |
27 | 10 | ||
28 | transactionRetryer( | 11 | return transactionRetryer( |
29 | function (callback) { | 12 | function (callback) { |
30 | return functionToRetry.apply(this, args.concat([ callback ])) | 13 | functionToRetry.apply(this, args) |
31 | }, | 14 | .then(result => callback(null, result)) |
32 | function (err) { | 15 | .catch(err => callback(err)) |
33 | if (err) { | ||
34 | logger.error(options.errorMessage, { error: err }) | ||
35 | } | ||
36 | |||
37 | // Do not return the error, continue the process | ||
38 | return finalCallback(null) | ||
39 | } | 16 | } |
40 | ) | 17 | ) |
18 | .catch(err => { | ||
19 | // Do not throw the error, continue the process | ||
20 | logger.error(options.errorMessage, { error: err }) | ||
21 | }) | ||
41 | } | 22 | } |
42 | 23 | ||
43 | function transactionRetryer (func: Function, callback: (err: Error) => void) { | 24 | function transactionRetryer (func: Function) { |
44 | retry({ | 25 | return new Promise((res, rej) => { |
45 | times: 5, | 26 | retry({ |
46 | 27 | times: 5, | |
47 | errorFilter: function (err) { | ||
48 | const willRetry = (err.name === 'SequelizeDatabaseError') | ||
49 | logger.debug('Maybe retrying the transaction function.', { willRetry }) | ||
50 | return willRetry | ||
51 | } | ||
52 | }, func, callback) | ||
53 | } | ||
54 | 28 | ||
55 | function startSerializableTransaction (callback: (err: Error, t: Sequelize.Transaction) => void) { | 29 | errorFilter: function (err) { |
56 | db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) { | 30 | const willRetry = (err.name === 'SequelizeDatabaseError') |
57 | // We force to return only two parameters | 31 | logger.debug('Maybe retrying the transaction function.', { willRetry }) |
58 | return callback(err, t) | 32 | return willRetry |
33 | } | ||
34 | }, func, function (err) { | ||
35 | err ? rej(err) : res() | ||
36 | }) | ||
59 | }) | 37 | }) |
60 | } | 38 | } |
61 | 39 | ||
62 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
63 | 41 | ||
64 | export { | 42 | export { |
65 | commitTransaction, | ||
66 | retryTransactionWrapper, | 43 | retryTransactionWrapper, |
67 | rollbackTransaction, | ||
68 | startSerializableTransaction, | ||
69 | transactionRetryer | 44 | transactionRetryer |
70 | } | 45 | } |