mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-20 06:07:57 +00:00
refactor: Only promisify read and write locks once
Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
9decea1d3b
commit
d5f4ac690f
@ -2,18 +2,15 @@ import * as Promise from 'bluebird';
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import { fs } from 'mz';
|
import { fs } from 'mz';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as Lock from 'rwlock';
|
|
||||||
|
|
||||||
import { ConfigSchema, ConfigValue } from '../lib/types';
|
import { ConfigSchema, ConfigValue } from '../lib/types';
|
||||||
|
import { readLock, writeLock } from '../lib/update-lock';
|
||||||
|
|
||||||
import * as constants from '../lib/constants';
|
import * as constants from '../lib/constants';
|
||||||
import { writeAndSyncFile, writeFileAtomic } from '../lib/fs-utils';
|
import { writeAndSyncFile, writeFileAtomic } from '../lib/fs-utils';
|
||||||
import * as osRelease from '../lib/os-release';
|
import * as osRelease from '../lib/os-release';
|
||||||
|
|
||||||
type LockCallback = (file: string) => Promise<() => void>;
|
|
||||||
|
|
||||||
export default class ConfigJsonConfigBackend {
|
export default class ConfigJsonConfigBackend {
|
||||||
private lock: Lock;
|
|
||||||
private readLockConfigJson: () => Promise.Disposer<() => void>;
|
private readLockConfigJson: () => Promise.Disposer<() => void>;
|
||||||
private writeLockConfigJson: () => Promise.Disposer<() => void>;
|
private writeLockConfigJson: () => Promise.Disposer<() => void>;
|
||||||
|
|
||||||
@ -25,12 +22,7 @@ export default class ConfigJsonConfigBackend {
|
|||||||
public constructor(schema: ConfigSchema, configPath?: string) {
|
public constructor(schema: ConfigSchema, configPath?: string) {
|
||||||
this.configPath = configPath;
|
this.configPath = configPath;
|
||||||
this.schema = schema;
|
this.schema = schema;
|
||||||
this.lock = new Lock();
|
|
||||||
|
|
||||||
const writeLock: LockCallback = Promise.promisify(
|
|
||||||
this.lock.async.writeLock,
|
|
||||||
);
|
|
||||||
const readLock: LockCallback = Promise.promisify(this.lock.async.readLock);
|
|
||||||
this.writeLockConfigJson = () =>
|
this.writeLockConfigJson = () =>
|
||||||
writeLock('config.json').disposer(release => release());
|
writeLock('config.json').disposer(release => release());
|
||||||
this.readLockConfigJson = () =>
|
this.readLockConfigJson = () =>
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
Promise = require 'bluebird'
|
Promise = require 'bluebird'
|
||||||
_ = require 'lodash'
|
_ = require 'lodash'
|
||||||
Lock = require 'rwlock'
|
|
||||||
EventEmitter = require 'events'
|
EventEmitter = require 'events'
|
||||||
fs = Promise.promisifyAll(require('fs'))
|
fs = Promise.promisifyAll(require('fs'))
|
||||||
express = require 'express'
|
express = require 'express'
|
||||||
@ -122,9 +121,8 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
@on 'error', (err) ->
|
@on 'error', (err) ->
|
||||||
console.error('Error in deviceState: ', err, err.stack)
|
console.error('Error in deviceState: ', err, err.stack)
|
||||||
@_currentVolatile = {}
|
@_currentVolatile = {}
|
||||||
_lock = new Lock()
|
@_writeLock = updateLock.writeLock
|
||||||
@_writeLock = Promise.promisify(_lock.async.writeLock)
|
@_readLock = updateLock.readLock
|
||||||
@_readLock = Promise.promisify(_lock.async.readLock)
|
|
||||||
@lastSuccessfulUpdate = null
|
@lastSuccessfulUpdate = null
|
||||||
@failedUpdates = 0
|
@failedUpdates = 0
|
||||||
@applyInProgress = false
|
@applyInProgress = false
|
||||||
|
@ -46,8 +46,14 @@ process.on('exit', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
type LockFn = (key: string | number) => Bluebird<() => void>;
|
||||||
const locker = new Lock();
|
const locker = new Lock();
|
||||||
const writeLock = Bluebird.promisify(locker.async.writeLock).bind(locker);
|
export const writeLock: LockFn = Bluebird.promisify(locker.async.writeLock, {
|
||||||
|
context: locker,
|
||||||
|
});
|
||||||
|
export const readLock: LockFn = Bluebird.promisify(locker.async.readLock, {
|
||||||
|
context: locker,
|
||||||
|
});
|
||||||
|
|
||||||
function dispose(release: () => void): Bluebird<void> {
|
function dispose(release: () => void): Bluebird<void> {
|
||||||
return Bluebird.map(_.keys(locksTaken), lockName => {
|
return Bluebird.map(_.keys(locksTaken), lockName => {
|
||||||
@ -101,5 +107,10 @@ export function lock(
|
|||||||
.disposer(dispose);
|
.disposer(dispose);
|
||||||
};
|
};
|
||||||
|
|
||||||
return Bluebird.using(takeTheLock(), fn);
|
const disposer = takeTheLock();
|
||||||
|
if (disposer) {
|
||||||
|
return Bluebird.using(disposer, fn);
|
||||||
|
} else {
|
||||||
|
return Bluebird.resolve(fn());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import * as Bluebird from 'bluebird';
|
import * as Bluebird from 'bluebird';
|
||||||
import * as es from 'event-stream';
|
import * as es from 'event-stream';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import * as Lock from 'rwlock';
|
|
||||||
|
|
||||||
import { EventTracker } from './event-tracker';
|
import { EventTracker } from './event-tracker';
|
||||||
import Docker = require('./lib/docker-utils');
|
import Docker = require('./lib/docker-utils');
|
||||||
import { LogType } from './lib/log-types';
|
import { LogType } from './lib/log-types';
|
||||||
|
import { writeLock } from './lib/update-lock';
|
||||||
import {
|
import {
|
||||||
LocalLogBackend,
|
LocalLogBackend,
|
||||||
LogBackend,
|
LogBackend,
|
||||||
@ -34,10 +34,6 @@ interface LoggerConstructOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
private writeLock: (key: string) => Bluebird<() => void> = Bluebird.promisify(
|
|
||||||
new Lock().async.writeLock,
|
|
||||||
);
|
|
||||||
|
|
||||||
private backend: LogBackend | null = null;
|
private backend: LogBackend | null = null;
|
||||||
private balenaBackend: BalenaLogBackend | null = null;
|
private balenaBackend: BalenaLogBackend | null = null;
|
||||||
private localBackend: LocalLogBackend | null = null;
|
private localBackend: LocalLogBackend | null = null;
|
||||||
@ -130,7 +126,7 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public lock(containerId: string): Bluebird.Disposer<() => void> {
|
public lock(containerId: string): Bluebird.Disposer<() => void> {
|
||||||
return this.writeLock(containerId).disposer(release => {
|
return writeLock(containerId).disposer(release => {
|
||||||
release();
|
release();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user