Prettiering

This commit is contained in:
Collin Duncan 2023-01-20 09:52:28 +01:00
parent dfcb1f25a1
commit a18a9ca34d
No known key found for this signature in database
4 changed files with 75 additions and 59 deletions

View file

@ -42,7 +42,9 @@ export const hashPassword = async (password: string) => {
const hash = await generateHash(password, saltBuffer) const hash = await generateHash(password, saltBuffer)
return hash return hash
} catch (err: any) { } catch (err: any) {
asyncLocalStorage.getStore()?.error('Error hashing and salting password', { message: err.message }) asyncLocalStorage
.getStore()
?.error('Error hashing and salting password', { message: err.message })
throw err throw err
} }
} }

View file

@ -54,7 +54,9 @@ export class Runner {
reservation.booked = true reservation.booked = true
return true return true
} catch (err) { } catch (err) {
asyncLocalStorage.getStore()?.error('Error making reservation', reservation.format()) asyncLocalStorage
.getStore()
?.error('Error making reservation', reservation.format())
return false return false
} }
} }
@ -77,7 +79,9 @@ export class Runner {
asyncLocalStorage.getStore()?.debug(`Navigating to ${date.format()}`) asyncLocalStorage.getStore()?.debug(`Navigating to ${date.format()}`)
if (this.getLastVisibleDay().isBefore(date)) { if (this.getLastVisibleDay().isBefore(date)) {
asyncLocalStorage.getStore()?.debug('Date is on different page, increase month') asyncLocalStorage
.getStore()
?.debug('Date is on different page, increase month')
await this.page?.waitForSelector('td.month.next').then((d) => d?.click()) await this.page?.waitForSelector('td.month.next').then((d) => d?.click())
} }
@ -96,7 +100,9 @@ export class Runner {
} }
private async selectAvailableTime(res: Reservation): Promise<void> { private async selectAvailableTime(res: Reservation): Promise<void> {
asyncLocalStorage.getStore()?.debug('Selecting available time', res.format()) asyncLocalStorage
.getStore()
?.debug('Selecting available time', res.format())
let freeCourt: ElementHandle | null | undefined let freeCourt: ElementHandle | null | undefined
let i = 0 let i = 0
while (i < res.possibleDates.length && !freeCourt) { while (i < res.possibleDates.length && !freeCourt) {

View file

@ -18,16 +18,21 @@ export const startTasks = () => {
const task = schedule( const task = schedule(
'0 * * * * *', '0 * * * * *',
async (timestamp) => { async (timestamp) => {
asyncLocalStorage.run(new Logger('cron', v4(), LogLevel.DEBUG), async () => { asyncLocalStorage.run(
const childLogger = asyncLocalStorage.getStore() new Logger('cron', v4(), LogLevel.DEBUG),
childLogger?.info('Running cron job', { timestamp }) async () => {
try { const childLogger = asyncLocalStorage.getStore()
await reserve() childLogger?.info('Running cron job', { timestamp })
childLogger?.info('Completed running cron job') try {
} catch (error: any) { await reserve()
childLogger?.error('Error running cron job', { error: error.message }) childLogger?.info('Completed running cron job')
} catch (error: any) {
childLogger?.error('Error running cron job', {
error: error.message,
})
}
} }
}) )
}, },
getTaskConfig('reserver cron') getTaskConfig('reserver cron')
) )

View file

@ -6,54 +6,57 @@ import { parseJson } from './utils'
// Handles POST requests to /reservations // Handles POST requests to /reservations
const server = http.createServer(async (req, res) => { const server = http.createServer(async (req, res) => {
await asyncLocalStorage.run(new Logger('request', v4(), LogLevel.DEBUG), async () => { await asyncLocalStorage.run(
const logger = asyncLocalStorage.getStore() new Logger('request', v4(), LogLevel.DEBUG),
logger?.debug('Incoming request') async () => {
const { url, method } = req const logger = asyncLocalStorage.getStore()
logger?.debug('Incoming request')
if ( const { url, method } = req
!url ||
!method || if (
!/^\/reservations$/.test(url) || !url ||
method.toLowerCase() !== 'post' !method ||
) { !/^\/reservations$/.test(url) ||
logger?.info('Not found') method.toLowerCase() !== 'post'
res.writeHead(404, 'Not found') ) {
logger?.info('Not found')
res.writeHead(404, 'Not found')
res.end()
return
}
let jsonBody: Record<string, unknown>
const contentType = req.headers['content-type'] || 'application/json'
if (contentType !== 'application/json') {
logger?.error('Invalid content type', { contentType })
res.writeHead(406, 'Unsupported content type')
res.end()
return
}
try {
const length = Number.parseInt(req.headers['content-length'] || '0')
const encoding = req.readableEncoding || 'utf8'
jsonBody = await parseJson(length, encoding, req)
} catch (error: any) {
logger?.error('Failed to parse body', { error: error.message })
res.writeHead(400, 'Bad request')
res.end()
return
}
try {
await schedule(jsonBody)
} catch (error: any) {
logger?.error('Failed to schedule request', { error })
res.writeHead(400, 'Bad request')
res.end()
return
}
res.end() res.end()
return
} }
)
let jsonBody: Record<string, unknown>
const contentType = req.headers['content-type'] || 'application/json'
if (contentType !== 'application/json') {
logger?.error('Invalid content type', { contentType })
res.writeHead(406, 'Unsupported content type')
res.end()
return
}
try {
const length = Number.parseInt(req.headers['content-length'] || '0')
const encoding = req.readableEncoding || 'utf8'
jsonBody = await parseJson(length, encoding, req)
} catch (error: any) {
logger?.error('Failed to parse body', { error: error.message })
res.writeHead(400, 'Bad request')
res.end()
return
}
try {
await schedule(jsonBody)
} catch (error: any) {
logger?.error('Failed to schedule request', { error })
res.writeHead(400, 'Bad request')
res.end()
return
}
res.end()
})
}) })
export default server export default server