Force trylock to write description to rtdb

This commit is contained in:
Garrett Mills 2021-04-10 14:50:02 -05:00
parent 1423e1961e
commit 092f3d49f1
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
5 changed files with 10 additions and 9 deletions

View File

@ -4,10 +4,9 @@ import {LocalFilesystem, LocalFilesystemConfig} from "@extollo/util"
export default { export default {
debug: env('DEBUG_MODE', false), debug: env('DEBUG_MODE', false),
base_url: env('BASE_URL', 'http://localhost:8000/'), base_url: env('BASE_URL', 'http://192.168.0.29:8000/'),
peers: [ peers: [
'http://127.0.0.1:8100/',
], ],
session: { session: {

View File

@ -281,7 +281,7 @@ export class Blockchain extends Unit {
this.isSubmitting = true this.isSubmitting = true
} }
await this.firebase.trylock('block') await this.firebase.trylock('block', 'Blockchain_refresh')
const validSeqID = (await this.read()).reverse()[0]?.seqID const validSeqID = (await this.read()).reverse()[0]?.seqID
const peers = await this.getPeers() const peers = await this.getPeers()

View File

@ -40,14 +40,16 @@ export class FirebaseUnit extends Unit {
* Try to lock the given database ref alias. * Try to lock the given database ref alias.
* Promise will sleep if lock is held, and will resolve once lock is acquired. * Promise will sleep if lock is held, and will resolve once lock is acquired.
* @param name * @param name
* @param description
*/ */
async trylock(name: RTDBRef): Promise<any> { async trylock(name: RTDBRef, description: string): Promise<any> {
return this._firebase.database() return this._firebase.database()
.ref(`${this.config.get('app.firebase.rtdb.refs.locks')}/${name}`) .ref(`${this.config.get('app.firebase.rtdb.refs.locks')}/${name}`)
.transaction(current => { .transaction(current => {
if ( !current || current.time < 1 ) { if ( !current || current.time < 1 ) {
return { return {
time: (new Date).getTime(), time: (new Date).getTime(),
description,
} }
} }
}, undefined, false).then(async result => { }, undefined, false).then(async result => {
@ -58,12 +60,12 @@ export class FirebaseUnit extends Unit {
this.logging.debug(`Unable to acquire lock: ${name}. Trying again soon...`) this.logging.debug(`Unable to acquire lock: ${name}. Trying again soon...`)
await this.sleep(500) await this.sleep(500)
return this.trylock(name) return this.trylock(name, description)
}) })
.catch(async reason => { .catch(async reason => {
this.logging.debug(`Unable to acquire lock: ${name}. Trying again soon...`) this.logging.debug(`Unable to acquire lock: ${name}. Trying again soon...`)
await this.sleep(500) await this.sleep(500)
return this.trylock(name) return this.trylock(name, description)
}) })
} }
@ -74,7 +76,7 @@ export class FirebaseUnit extends Unit {
async unlock(name: RTDBRef) { async unlock(name: RTDBRef) {
await this._firebase.database() await this._firebase.database()
.ref(`${this.config.get('app.firebase.rtdb.refs.locks')}/${name}`) .ref(`${this.config.get('app.firebase.rtdb.refs.locks')}/${name}`)
.set({time: 0}, err => { .set({time: 0, description: 'none'}, err => {
if ( err ) this.logging.error(err) if ( err ) this.logging.error(err)
}) })
} }

View File

@ -47,7 +47,7 @@ export class Exposure extends Unit {
this.firebase.ref('exposure').on('child_added', async (snapshot) => { this.firebase.ref('exposure').on('child_added', async (snapshot) => {
this.logging.debug('Received child_added event for exposures reference.') this.logging.debug('Received child_added event for exposures reference.')
if ( !this.claim() ) return if ( !this.claim() ) return
await this.firebase.trylock('block') await this.firebase.trylock('block', 'Exposure_child_added')
const exposure: ExposureResourceItem = snapshot.val() const exposure: ExposureResourceItem = snapshot.val()

View File

@ -77,7 +77,7 @@ export class Transaction extends Unit {
this.firebase.ref("transaction").on("child_added", async () => { this.firebase.ref("transaction").on("child_added", async () => {
this.logging.debug('Received child_added event for transactions reference.') this.logging.debug('Received child_added event for transactions reference.')
if ( !this.claim() ) return if ( !this.claim() ) return
await this.firebase.trylock('block') await this.firebase.trylock('block', 'Transaction_child_added')
// array of pairs of transaction resource items // array of pairs of transaction resource items
let groupedTransactions: [TransactionResourceItem, TransactionResourceItem][] = [] let groupedTransactions: [TransactionResourceItem, TransactionResourceItem][] = []