Wake up free instance

So far, I have found lots of information about keeping an instance alive using cron. However, my app only needs to run once a day at a specific time, so I don’t need to keep it alive 24/7. Instead, I would like a way to wake it up for a specific window so that I can trigger the action during that window. Does anyone have any suggestions for automating this? I’ve tried calling a GET using cron to wake it up, but the GET times out and doesn’t end up waking up the instance. Thanks.

You can use cron for this I think, you just need to specify the time of day you need the instance to wake up. The other posts are probably focused on “waking up” a web servcie with a request every 14 minutes or so, but for your use case, just set the job to run at the desire hour, for example this syntax will wake up your instance every day at 5:15PM:

15 17 * * *

..keep in mind that Render cron jobs run in the UTC timezone, if that’s what you’re using.

But what call should I make? I tried a GET and a POST, but both timeout after 30s of no response. I expect that the server will be available soon after that, but that doesn’t seem to be the result I am getting. It’s like the server knows the request has timed out and so abandons the startup.

I thought about this a bit more, what Render is doing here is loading their page, waking up your instance very slowly, then eventually showing your page. Once the instance is launched, the page responds more quickly. I don’t think using curl or similar will work, you need to make the request with a browser. Luckily, you can use puppeteer as a headless browser and scan for changes in the page’s title element value, something like this script:

import puppeteer from 'puppeteer'
import 'dotenv/config'

if (!process.argv[2]) {
	console.error('Usage: node index.js "<intended-title>"')
	console.error('Please provide the intended title as a command line argument.')
	process.exit(1)
}

let IntendedTitle = process.argv[2]

function checkTitle(title) {
	if (title === IntendedTitle) {
		console.log(`✅ Title matches: "${title}", site is now live!`)
		process.exit(0) // Exit the script if the title matches
	}
}

async function loadWebsite(url) {
	console.log(`Loading website: ${url}`)

	// Launch browser
	const browser = await puppeteer.launch({
		headless: true, // Set to false if you want to see the browser
		args: ['--no-sandbox', '--disable-setuid-sandbox'],
	})

	let page = null
	let title = null
	try {
		// Create a new page
		page = await browser.newPage()

		// Set user agent to avoid bot detection
		await page.setUserAgent(
			'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
		)

		// Navigate to the website
		const response = await page.goto(url, {
			waitUntil: 'networkidle2',
			timeout: 30000,
		})

		if (response.ok()) {
			console.log(`✅ Successfully loaded ${url}`)
			console.log(`Status: ${response.status()}`)

			// Get page title
			title = await page.title()
			// console.log(`Page title: ${title}`)

			checkTitle(title)
		} else {
			console.log(`❌ Failed to load ${url}`)
			console.log(`Status: ${response.status()}`)
		}
	} catch (error) {
		console.error(`Error loading ${url}:`, error.message)
	}

	setInterval(async () => {
		// Keep the script running to prevent the browser from closing
		console.log(`Keeping the browser alive...`)
		title = await page.title()

		if (title) {
			// console.log(`Page title: ${title}`)
			checkTitle(title)
		}
	}, 2000)

	setTimeout(() => {
		console.log(`Waiting for 90 seconds before closing the browser...`)
		browser.close()
		console.log('Browser closed.')
	}, 90000)
}

const URL = 'https://wake-test.onrender.com'

export default async function main() {
	// Get URL from environment variable or use default
	const url = process.env.TARGET_URL || URL

	console.log('Puppeteer Website Loader')
	console.log('========================')

	await loadWebsite(url)
}

// Run the main function
main().catch(console.error)

It seems to take the better part of a minute to wake the instance up. I think your use case fits the render terms of service - running a cron task to wake up the instance using this script once a day, I assume to post some data to?

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.