Feat: make lamassu-migrate use async local storage

This commit is contained in:
csrapr 2021-03-02 17:33:47 +00:00 committed by Josh Harvey
parent 351d170c31
commit 7bbf2829de
5 changed files with 26 additions and 26 deletions

View file

@ -4,7 +4,7 @@ const FileStore = require('migrate/lib/file-store')
const db = require('../lib/db')
const migrate = require('../lib/migrate')
const options = require('../lib/options')
const { asyncLocalStorage, defaultStore } = require('../lib/async-storage')
const createMigration = `CREATE TABLE IF NOT EXISTS migrations (
id serial PRIMARY KEY,
data json NOT NULL
@ -21,6 +21,8 @@ const getMigrateFile = () => {
})
}
const store = defaultStore()
asyncLocalStorage.run(store, () => {
db.$none(createMigration)
.then(() => Promise.all([db.$oneOrNone(select), getMigrateFile()]))
.then(([qResult, migrateFile]) => {
@ -38,3 +40,4 @@ db.$none(createMigration)
console.error('DB Migration failed: %s', err)
process.exit(1)
})
})

View file

@ -1,11 +1,10 @@
const { AsyncLocalStorage } = require('async_hooks')
const asyncLocalStorage = new AsyncLocalStorage()
const defaultStore = (a = null) => {
const defaultStore = () => {
const store = new Map()
store.set('schema', 'public')
store.set('defaultSchema', 'ERROR_SCHEMA')
if (a) store.set('a', 'a')
return store
}

View file

@ -91,7 +91,7 @@ eventBus.subscribe('log', args => {
(id, device_id, message, log_level, meta) values ($1, $2, $3, $4, $5) returning *`
// need to set AsyncLocalStorage (ALS) for this function as well
// because this module is imported before ALS is set up on app.js
const store = defaultStore(true)
const store = defaultStore()
asyncLocalStorage.run(store, () => {
db.$one(sql, [uuid.v4(), '', msgToSave, level, meta])
.then(_.mapKeys(_.camelCase))

View file

@ -11,7 +11,7 @@ const cookieParser = require('cookie-parser')
const { ApolloServer, AuthenticationError } = require('apollo-server-express')
const _ = require('lodash/fp')
const asyncLocalStorage = require('../async-storage')
const { asyncLocalStorage, defaultStore } = require('../async-storage')
const options = require('../options')
const users = require('../users')
@ -97,10 +97,8 @@ const certOptions = {
}
function run () {
const store = new Map()
const store = defaultStore()
asyncLocalStorage.run(store, () => {
store.set('schema', 'public')
store.set('defaultSchema', 'ERROR_SCHEMA')
const serverPort = devMode ? 8070 : 443
const serverLog = `lamassu-admin-server listening on port ${serverPort}`