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)
return hash
} 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
}
}

View file

@ -54,7 +54,9 @@ export class Runner {
reservation.booked = true
return true
} catch (err) {
asyncLocalStorage.getStore()?.error('Error making reservation', reservation.format())
asyncLocalStorage
.getStore()
?.error('Error making reservation', reservation.format())
return false
}
}
@ -77,7 +79,9 @@ export class Runner {
asyncLocalStorage.getStore()?.debug(`Navigating to ${date.format()}`)
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())
}
@ -96,7 +100,9 @@ export class Runner {
}
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 i = 0
while (i < res.possibleDates.length && !freeCourt) {

View file

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

View file

@ -6,54 +6,57 @@ import { parseJson } from './utils'
// Handles POST requests to /reservations
const server = http.createServer(async (req, res) => {
await asyncLocalStorage.run(new Logger('request', v4(), LogLevel.DEBUG), async () => {
const logger = asyncLocalStorage.getStore()
logger?.debug('Incoming request')
const { url, method } = req
await asyncLocalStorage.run(
new Logger('request', v4(), LogLevel.DEBUG),
async () => {
const logger = asyncLocalStorage.getStore()
logger?.debug('Incoming request')
const { url, method } = req
if (
!url ||
!method ||
!/^\/reservations$/.test(url) ||
method.toLowerCase() !== 'post'
) {
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
}
if (
!url ||
!method ||
!/^\/reservations$/.test(url) ||
method.toLowerCase() !== 'post'
) {
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()
})
)
})
export default server