Skip to content

Commit

Permalink
refactor startup and shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
RebootJeff committed Feb 25, 2021
1 parent ea3dc3e commit 2c3cf46
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import notify from './notification/index.js'
import sites from './sites/index.js'
import wait from './utils/wait.js'
import {
LOOP_INTERVAL, SEARCH,
} from '../privateConfig.js'
Expand Down Expand Up @@ -57,7 +58,7 @@ const startChecking = async (browser) => {

const pauseDuration = Math.round(INTERVAL_MS / sites.length)
console.log(`⏳ Sites will be checked again in ~${Math.round(pauseDuration / 1000)} seconds.`)
await page.waitForTimeout(pauseDuration)
await wait(pauseDuration)
}
}

Expand Down
49 changes: 28 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import puppeteer from 'puppeteer'
import readline from 'readline'

import logger from './utils/logger.js'
import startChecking from './check.js'
import {
HIDE_BROWSER, BROWSER_SIZE,
} from '../privateConfig.js'

// TODO: This isn't working as intended. I'm not sure it's even being invoked.
const shutDown = async (browser) => {
logger.log('🏁 Closing browser...')
await browser.close()
process.exit(0)
let browser

// TODO: On Windows, there is a long pause between the browser closing and "Closing browser..."
// getting logged. Somehow CTRL+C stops Puppeteer browser before this function runs.
// And why is there such a long pause?
const shutDown = async (code = 0) => {
if (browser) {
logger.log('🏁 Closing browser...')
await browser.close() // TODO: Node doesn't seem to wait for this.
console.log('why doesn\'t this show up?')
}
process.exit(code)
}

process.on('SIGINT', shutDown)
process.on('SIGQUIT', shutDown)
process.on('SIGTERM', shutDown)

process.on('exit', () => {
// TODO: Inspect the `code` parameter and log accordingly
logger.log('👋 Shutdown completed! Bye bye!')
})

// Here's where the app begins 🚀
(async () => {
const start = async () => {
logger.log('🚦 Launching browser...')
const { height, width } = BROWSER_SIZE
const browser = await puppeteer.launch({
browser = await puppeteer.launch({
headless: HIDE_BROWSER,
defaultViewport: { width, height },
args: [`--window-size=${width},${height}`]
})

process.on('SIGINT', () => shutDown(browser))
process.on('SIGQUIT', () => shutDown(browser))
process.on('SIGTERM', () => shutDown(browser))

// TODO: Inspect the `code` parameter and log accordingly
process.on('exit', () => {
logger.log('👋 Shutdown completed! Bye bye!')
});

try {
startChecking(browser)
} catch (err) {
logger.error('💥 Loop failure:', err)
await shutDown()
process.exit(1)
logger.error('💥 App failure:', err)
await shutDown(1)
}
})();
}

start()
3 changes: 3 additions & 0 deletions src/utils/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const wait = duration => new Promise(resolve => setTimeout(resolve, duration))

export default wait

0 comments on commit 2c3cf46

Please sign in to comment.