Skip to content

Commit

Permalink
guard all shell executions
Browse files Browse the repository at this point in the history
  • Loading branch information
rush42 committed Nov 13, 2024
1 parent beb3b02 commit 1453af9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions processing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { writeMetadata } from './steps/metadata'
import { processTopics } from './steps/processTopics'
import { setup } from './steps/setup'
import { params } from './utils/parameters'
import { logError } from './utils/synology'
import { synologyLogError } from './utils/synology'

try {
// setup directories and backup schema
Expand Down Expand Up @@ -65,5 +65,5 @@ try {
}
} catch (e) {
console.error(e)
logError('Processing failed')
synologyLogError('Processing failed')
}
6 changes: 4 additions & 2 deletions processing/steps/download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { basename, join } from 'path'
import { OSM_DOWNLOAD_DIR } from '../constants/directories.const'
import { readPersistent, writePersistent } from '../utils/persistentData'
import { logError } from '../utils/synology'
import { synologyLogError } from '../utils/synology'

/**
* Get the full path to the downloaded file.
Expand Down Expand Up @@ -37,7 +37,9 @@ export async function waitForFreshData(fileURL: URL, maxTries: number, timeoutMi
tries++
// if we exceeded the maximum number of tries, return false and log to synlogy
if (tries >= maxTries) {
logError(`Timeout exceeded while waiting for fresh data. File is from ${lastModifiedDate}`)
synologyLogError(
`Timeout exceeded while waiting for fresh data. File is from ${lastModifiedDate}`,
)
return false
}

Expand Down
12 changes: 8 additions & 4 deletions processing/steps/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export async function createMetadataTable() {
/**
* Get the timestamp from the given OSM file. Uses osmium fileinfo to get the timestamp.
* @param fileName the file name to take the timestamp from
* @returns
* @returns the time stamp as a string
*/
export async function getFileTimestamp(fileName: string) {
return $`osmium fileinfo ${filteredFilePath(fileName)} -g header.option.timestamp`
.text()
.then((timestamp) => timestamp.trim())
try {
const timestamp =
await $`osmium fileinfo ${filteredFilePath(fileName)} -g header.option.timestamp`.text()
return timestamp.trim()
} catch {
throw new Error(`Failed to get timestamp from file ${fileName}`)
}
}

/**
Expand Down
25 changes: 16 additions & 9 deletions processing/steps/processTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ async function runSQL(topic: Topic) {
const psqlFile = `${mainFilePath(topic)}.sql`

if (await Bun.file(psqlFile).exists()) {
return $`psql -q -f ${psqlFile}`
try {
await $`psql -q -f ${psqlFile}`
} catch {
throw new Error(`Failed to run SQL file ${psqlFile}`)
}
}
}

Expand All @@ -38,14 +42,17 @@ async function runSQL(topic: Topic) {
async function runLua(fileName: string, topic: Topic) {
const filePath = filteredFilePath(fileName)
const luaFile = `${mainFilePath(topic)}.lua`

return $`osm2pgsql \
--number-processes=8 \
--create \
--output=flex \
--extra-attributes \
--style=${luaFile} \
${filePath}`
try {
await $`osm2pgsql \
--number-processes=8 \
--create \
--output=flex \
--extra-attributes \
--style=${luaFile} \
${filePath}`
} catch {
throw new Error(`Failed to run lua file ${luaFile}`)
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions processing/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import { formatTimestamp } from './formatTimestamp'
import { logInfo } from './synology'
import { synologyLogInfo } from './synology'
import { endTimer, startTimer } from './timeTracking'

const lineLength = process.stdout.columns || 120
Expand All @@ -10,15 +10,15 @@ export function logPadded(left: string, right: string = '') {
}
export function logStart(id: string) {
const message = `${id} started`
logInfo(message)
synologyLogInfo(message)
logPadded(message)
startTimer(id)
}

export function logEnd(id: string) {
const timeElapsed = endTimer(id)
const message = `${id} finished`
logInfo(message)
synologyLogInfo(message)
logPadded(message, formatTimestamp(timeElapsed))
return timeElapsed
}
15 changes: 13 additions & 2 deletions processing/utils/synology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@ async function logToSynology(message: string, token: string) {
})
}

export async function logInfo(message: string) {
/**
* Log an info message to the synology chat
* @param message
* @returns
*/

export async function synologyLogInfo(message: string) {
if (!params.synologyLogToken) {
return
}
await logToSynology(message, params.synologyLogToken)
}

export async function logError(message: string) {
/**
* Log an error message to the synology chat
* @param message
* @returns
*/
export async function synologyLogError(message: string) {
if (!params.synologyErrorLogToken) {
return
}
Expand Down

0 comments on commit 1453af9

Please sign in to comment.