keepalive-render 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/bin/keepalive.js +112 -0
- package/lib/args.js +167 -0
- package/lib/background.js +63 -0
- package/lib/cli.js +109 -0
- package/lib/ping.js +53 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aule Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,147 @@
|
|
|
1
|
-
#
|
|
1
|
+
# keepalive-render
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Keep a free-tier [Render](https://render.com) service awake by pinging any `*.onrender.com` URL every 10 minutes.
|
|
4
|
+
|
|
5
|
+
Render spins free web services down after ~15 minutes without traffic, and the next visitor waits 50+ seconds for a cold start. One request every 10 minutes is enough to keep the instance warm.
|
|
6
|
+
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx keepalive-render
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
It asks one question, then gets out of your way:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
Which Render app should I keep awake? myapp.onrender.com
|
|
17
|
+
|
|
18
|
+
Running in the background (pid 48213).
|
|
19
|
+
|
|
20
|
+
log ~/.keepalive-render/myapp.onrender.com.log
|
|
21
|
+
stop kill 48213
|
|
22
|
+
|
|
23
|
+
Watch it with: tail -f ~/.keepalive-render/myapp.onrender.com.log
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
That is the whole workflow. The monitor runs detached, so the terminal is free and the pings continue after you close it.
|
|
27
|
+
|
|
28
|
+
Requires Node.js 18 or newer. No dependencies.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
keepalive-render [your-app] [options]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The app can be given in any of these forms — `https` is assumed:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
keepalive-render # asks for the URL
|
|
40
|
+
keepalive-render myapp
|
|
41
|
+
keepalive-render myapp.onrender.com
|
|
42
|
+
keepalive-render https://myapp.onrender.com
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Options
|
|
46
|
+
|
|
47
|
+
| Flag | Default | Description |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `-i, --every <time>` | `10m` | Time between pings. Accepts `90s`, `1h`, `500ms`, or a bare number of minutes. |
|
|
50
|
+
| `-t, --timeout <time>` | `30s` | Per-request timeout. |
|
|
51
|
+
| `-p, --path <path>` | the URL's own path | Path to request, e.g. `/health`. |
|
|
52
|
+
| `-f, --foreground` | off | Watch pings live in the terminal instead of detaching. |
|
|
53
|
+
| `-b, --background` | on | Run detached (the default; the flag is there if you want to be explicit). |
|
|
54
|
+
| `--once` | off | Ping once and exit: `0` on success, `1` on failure. |
|
|
55
|
+
| `-q, --quiet` | off | Only print failures. |
|
|
56
|
+
| `-v, --version` | | Print the version. |
|
|
57
|
+
| `-h, --help` | | Print help. |
|
|
58
|
+
|
|
59
|
+
### Examples
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
keepalive-render myapp --every 5m --path /health
|
|
63
|
+
keepalive-render myapp --foreground # watch it, Ctrl+C to stop
|
|
64
|
+
keepalive-render myapp --once --quiet && echo awake
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Watching the pings
|
|
68
|
+
|
|
69
|
+
Add `--foreground` to hold the terminal and see every request:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
keepalive-render myapp --foreground
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
Keeping myapp.onrender.com awake every 10m · Ctrl+C to stop
|
|
77
|
+
|
|
78
|
+
16:04:48 ✓ 200 120ms
|
|
79
|
+
16:14:49 ✓ 200 35ms
|
|
80
|
+
16:24:51 ✗ 503 1.2s
|
|
81
|
+
16:34:53 ✓ 200 98ms back up
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Failures never stop the loop — a deploy or cold start is just a red line. `Ctrl+C` stops it.
|
|
85
|
+
|
|
86
|
+
## Background mode
|
|
87
|
+
|
|
88
|
+
Detached is the default. Everything it prints goes to a log, and a pidfile sits next to it:
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
~/.keepalive-render/myapp.onrender.com.log
|
|
92
|
+
~/.keepalive-render/myapp.onrender.com.pid
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
tail -f ~/.keepalive-render/myapp.onrender.com.log # watch it
|
|
97
|
+
kill $(cat ~/.keepalive-render/myapp.onrender.com.pid) # stop it
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Starting the same app twice is safe — the second run reports the PID that is already running instead of starting another monitor.
|
|
101
|
+
|
|
102
|
+
### Across reboots
|
|
103
|
+
|
|
104
|
+
Background mode survives closing the terminal, not a reboot. To bring it back automatically:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# pm2
|
|
108
|
+
npm install -g pm2
|
|
109
|
+
pm2 start keepalive-render --name keepalive-myapp -- myapp.onrender.com --foreground
|
|
110
|
+
pm2 save && pm2 startup
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```ini
|
|
114
|
+
# systemd (Linux): /etc/systemd/system/keepalive-myapp.service
|
|
115
|
+
[Unit]
|
|
116
|
+
Description=keepalive-render
|
|
117
|
+
After=network-online.target
|
|
118
|
+
|
|
119
|
+
[Service]
|
|
120
|
+
ExecStart=/usr/bin/npx keepalive-render myapp.onrender.com --foreground
|
|
121
|
+
Restart=always
|
|
122
|
+
|
|
123
|
+
[Install]
|
|
124
|
+
WantedBy=multi-user.target
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
If you would rather not run a process at all, point a free uptime pinger (UptimeRobot, cron-job.org, a GitHub Actions schedule) at the same URL every 10 minutes.
|
|
128
|
+
|
|
129
|
+
## Notes
|
|
130
|
+
|
|
131
|
+
- A ping is a normal `GET` with a `keepalive-render` user agent; the response body is drained so the connection closes cleanly.
|
|
132
|
+
- Render's free tier includes 750 instance hours per month. Keeping one service awake around the clock uses most of that, which is the intended trade.
|
|
133
|
+
- This keeps a service warm; it does not stop Render from redeploying, sleeping on its own schedule, or failing health checks. Render's terms and free-tier behaviour are Render's to change.
|
|
134
|
+
|
|
135
|
+
## Publishing to npm
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npm login # once, as the account that owns the name
|
|
139
|
+
npm view keepalive-render # confirm the name is still free
|
|
140
|
+
npm publish --access public
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Bump the version in `package.json` (`npm version patch`) and run `npm publish` again for each release. To publish under a scope instead, rename the package to `@aule-labs/keepalive-render` and keep `--access public` (scoped packages are private by default).
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT © Aule Labs
|
package/bin/keepalive.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import { createInterface } from 'node:readline/promises'
|
|
4
|
+
import { HELP, UsageError, parseArgs, resolveTarget } from '../lib/args.js'
|
|
5
|
+
import { startBackground, shortenPath } from '../lib/background.js'
|
|
6
|
+
import { monitor } from '../lib/cli.js'
|
|
7
|
+
|
|
8
|
+
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
9
|
+
|
|
10
|
+
async function askForApp() {
|
|
11
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout })
|
|
12
|
+
try {
|
|
13
|
+
for (;;) {
|
|
14
|
+
const answer = (await rl.question('Which Render app should I keep awake? ')).trim()
|
|
15
|
+
if (!answer) return null
|
|
16
|
+
try {
|
|
17
|
+
return resolveTarget(answer)
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (!(err instanceof UsageError)) throw err
|
|
20
|
+
console.error(` ${err.message}`)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
return null
|
|
25
|
+
} finally {
|
|
26
|
+
rl.close()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function main() {
|
|
31
|
+
let opts
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
opts = parseArgs(process.argv.slice(2))
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (err instanceof UsageError) {
|
|
37
|
+
console.error(`error: ${err.message}\n`)
|
|
38
|
+
console.error(HELP)
|
|
39
|
+
return 2
|
|
40
|
+
}
|
|
41
|
+
throw err
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (opts.help) {
|
|
45
|
+
console.log(HELP)
|
|
46
|
+
return 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (opts.version) {
|
|
50
|
+
console.log(pkg.version)
|
|
51
|
+
return 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let target = null
|
|
55
|
+
|
|
56
|
+
if (opts.target) {
|
|
57
|
+
try {
|
|
58
|
+
target = resolveTarget(opts.target)
|
|
59
|
+
} catch (err) {
|
|
60
|
+
if (err instanceof UsageError) {
|
|
61
|
+
console.error(`error: ${err.message}`)
|
|
62
|
+
return 2
|
|
63
|
+
}
|
|
64
|
+
throw err
|
|
65
|
+
}
|
|
66
|
+
} else if (process.stdin.isTTY) {
|
|
67
|
+
target = await askForApp()
|
|
68
|
+
if (!target) {
|
|
69
|
+
console.log('Nothing to keep awake. Bye.')
|
|
70
|
+
return 0
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
console.log(HELP)
|
|
74
|
+
return 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (opts.once || opts.foreground) {
|
|
78
|
+
return monitor(opts, target, pkg.version)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Detached by default: the terminal stays free. The child gets the resolved
|
|
82
|
+
// target explicitly, since it has no TTY to prompt on and no argv of its own.
|
|
83
|
+
const args = [
|
|
84
|
+
target.toString(),
|
|
85
|
+
'--foreground',
|
|
86
|
+
'--every',
|
|
87
|
+
`${opts.intervalMs}ms`,
|
|
88
|
+
'--timeout',
|
|
89
|
+
`${opts.timeoutMs}ms`,
|
|
90
|
+
]
|
|
91
|
+
if (opts.path) args.push('--path', opts.path)
|
|
92
|
+
if (opts.quiet) args.push('--quiet')
|
|
93
|
+
|
|
94
|
+
const result = startBackground(args, target)
|
|
95
|
+
const log = shortenPath(result.paths.log)
|
|
96
|
+
|
|
97
|
+
console.log(
|
|
98
|
+
result.alreadyRunning
|
|
99
|
+
? `Already running in the background (pid ${result.pid}).`
|
|
100
|
+
: `Running in the background (pid ${result.pid}).`,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
console.log(`\n log ${log}\n stop kill ${result.pid}\n\nWatch it with: tail -f ${log}`)
|
|
104
|
+
return 0
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
main()
|
|
108
|
+
.then((code) => process.exit(code))
|
|
109
|
+
.catch((err) => {
|
|
110
|
+
console.error(err?.stack || String(err))
|
|
111
|
+
process.exit(1)
|
|
112
|
+
})
|
package/lib/args.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
export class UsageError extends Error {}
|
|
2
|
+
|
|
3
|
+
const DEFAULT_INTERVAL_MS = 10 * 60 * 1000
|
|
4
|
+
const DEFAULT_TIMEOUT_MS = 30 * 1000
|
|
5
|
+
|
|
6
|
+
export const HELP = `keepalive-render — keep a Render app awake
|
|
7
|
+
|
|
8
|
+
keepalive-render [your-app] [options]
|
|
9
|
+
|
|
10
|
+
With no app it asks for the URL. Or pass myapp, myapp.onrender.com
|
|
11
|
+
or a full https:// URL.
|
|
12
|
+
|
|
13
|
+
-i, --every <time> how often to ping (default: 10m)
|
|
14
|
+
-t, --timeout <time> give up on a ping after (default: 30s)
|
|
15
|
+
-p, --path <path> request this path instead (default: /)
|
|
16
|
+
-f, --foreground watch pings live instead of running detached
|
|
17
|
+
-b, --background run detached (default)
|
|
18
|
+
--once ping once, then exit
|
|
19
|
+
-q, --quiet only show failures
|
|
20
|
+
-v, --version
|
|
21
|
+
-h, --help
|
|
22
|
+
|
|
23
|
+
Examples
|
|
24
|
+
keepalive-render
|
|
25
|
+
keepalive-render myapp
|
|
26
|
+
keepalive-render myapp --foreground --every 5m
|
|
27
|
+
keepalive-render myapp --once
|
|
28
|
+
|
|
29
|
+
Render sleeps free apps after ~15 minutes of no traffic. A ping every
|
|
30
|
+
10 minutes keeps yours awake.`
|
|
31
|
+
|
|
32
|
+
export function parseArgs(argv) {
|
|
33
|
+
const opts = {
|
|
34
|
+
target: null,
|
|
35
|
+
intervalMs: DEFAULT_INTERVAL_MS,
|
|
36
|
+
timeoutMs: DEFAULT_TIMEOUT_MS,
|
|
37
|
+
path: null,
|
|
38
|
+
background: false,
|
|
39
|
+
foreground: false,
|
|
40
|
+
once: false,
|
|
41
|
+
quiet: false,
|
|
42
|
+
help: false,
|
|
43
|
+
version: false,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const args = [...argv]
|
|
47
|
+
|
|
48
|
+
while (args.length > 0) {
|
|
49
|
+
let arg = args.shift()
|
|
50
|
+
|
|
51
|
+
// support --flag=value
|
|
52
|
+
let inlineValue = null
|
|
53
|
+
if (arg.startsWith('--') && arg.includes('=')) {
|
|
54
|
+
const index = arg.indexOf('=')
|
|
55
|
+
inlineValue = arg.slice(index + 1)
|
|
56
|
+
arg = arg.slice(0, index)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const value = (name) => {
|
|
60
|
+
if (inlineValue !== null) return inlineValue
|
|
61
|
+
const next = args.shift()
|
|
62
|
+
if (next === undefined) throw new UsageError(`${name} needs a value`)
|
|
63
|
+
return next
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
switch (arg) {
|
|
67
|
+
case '-h':
|
|
68
|
+
case '--help':
|
|
69
|
+
opts.help = true
|
|
70
|
+
break
|
|
71
|
+
case '-v':
|
|
72
|
+
case '--version':
|
|
73
|
+
opts.version = true
|
|
74
|
+
break
|
|
75
|
+
case '--once':
|
|
76
|
+
opts.once = true
|
|
77
|
+
break
|
|
78
|
+
case '-b':
|
|
79
|
+
case '--background':
|
|
80
|
+
opts.background = true
|
|
81
|
+
break
|
|
82
|
+
case '-f':
|
|
83
|
+
case '--foreground':
|
|
84
|
+
opts.foreground = true
|
|
85
|
+
break
|
|
86
|
+
case '-q':
|
|
87
|
+
case '--quiet':
|
|
88
|
+
opts.quiet = true
|
|
89
|
+
break
|
|
90
|
+
case '-i':
|
|
91
|
+
case '--every':
|
|
92
|
+
case '--interval':
|
|
93
|
+
opts.intervalMs = parseDuration(value('--every'), 'interval')
|
|
94
|
+
break
|
|
95
|
+
case '-t':
|
|
96
|
+
case '--timeout':
|
|
97
|
+
opts.timeoutMs = parseDuration(value('--timeout'), 'timeout')
|
|
98
|
+
break
|
|
99
|
+
case '-p':
|
|
100
|
+
case '--path':
|
|
101
|
+
opts.path = normalizePath(value('--path'))
|
|
102
|
+
break
|
|
103
|
+
default:
|
|
104
|
+
if (arg.startsWith('-')) throw new UsageError(`unknown option: ${arg}`)
|
|
105
|
+
if (opts.target) throw new UsageError(`unexpected argument: ${arg}`)
|
|
106
|
+
opts.target = arg
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (inlineValue !== null) {
|
|
110
|
+
const takesValue = ['-i', '--every', '--interval', '-t', '--timeout', '-p', '--path']
|
|
111
|
+
if (!takesValue.includes(arg)) throw new UsageError(`${arg} does not take a value`)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return opts
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// "10" -> 10 minutes, "90s" -> 90 seconds, "1h" -> 1 hour, "500ms" -> 500 ms
|
|
119
|
+
export function parseDuration(input, name = 'duration') {
|
|
120
|
+
const match = /^(\d+(?:\.\d+)?)\s*(ms|s|m|h)?$/i.exec(String(input).trim())
|
|
121
|
+
if (!match) throw new UsageError(`invalid ${name}: ${input} (try 10, 90s or 1h)`)
|
|
122
|
+
|
|
123
|
+
const amount = Number(match[1])
|
|
124
|
+
const unit = (match[2] || 'm').toLowerCase()
|
|
125
|
+
const multipliers = { ms: 1, s: 1000, m: 60_000, h: 3_600_000 }
|
|
126
|
+
const ms = amount * multipliers[unit]
|
|
127
|
+
|
|
128
|
+
if (!Number.isFinite(ms) || ms < 100) {
|
|
129
|
+
throw new UsageError(`${name} is too small: ${input} (minimum 100ms)`)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return Math.round(ms)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function normalizePath(input) {
|
|
136
|
+
const raw = String(input).trim()
|
|
137
|
+
if (!raw) return '/'
|
|
138
|
+
return raw.startsWith('/') ? raw : `/${raw}`
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function resolveTarget(input) {
|
|
142
|
+
if (!input) throw new UsageError('no app given')
|
|
143
|
+
|
|
144
|
+
let raw = String(input).trim()
|
|
145
|
+
if (!raw) throw new UsageError('no app given')
|
|
146
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) raw = `https://${raw}`
|
|
147
|
+
|
|
148
|
+
let url
|
|
149
|
+
try {
|
|
150
|
+
url = new URL(raw)
|
|
151
|
+
} catch {
|
|
152
|
+
throw new UsageError(`that does not look like a URL: ${input}`)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
156
|
+
throw new UsageError(`only http:// and https:// work, got ${url.protocol}`)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// "myapp" (no dot) is shorthand for a Render service
|
|
160
|
+
if (!url.hostname.includes('.') && url.hostname !== 'localhost') {
|
|
161
|
+
url.hostname = `${url.hostname}.onrender.com`
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!url.pathname) url.pathname = '/'
|
|
165
|
+
|
|
166
|
+
return url
|
|
167
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
openSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs'
|
|
10
|
+
import { homedir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
import { fileURLToPath } from 'node:url'
|
|
13
|
+
|
|
14
|
+
const ENTRY = fileURLToPath(new URL('../bin/keepalive.js', import.meta.url))
|
|
15
|
+
const DIR = join(homedir(), '.keepalive-render')
|
|
16
|
+
|
|
17
|
+
export function statePaths(target) {
|
|
18
|
+
const name = `${target.hostname}${target.pathname}`.replace(/[^\w.-]+/g, '_').replace(/[._]+$/, '')
|
|
19
|
+
return {
|
|
20
|
+
pid: join(DIR, `${name}.pid`),
|
|
21
|
+
log: join(DIR, `${name}.log`),
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readPid(paths) {
|
|
26
|
+
if (!existsSync(paths.pid)) return null
|
|
27
|
+
const pid = Number(readFileSync(paths.pid, 'utf8').trim())
|
|
28
|
+
return Number.isInteger(pid) && pid > 0 ? pid : null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isRunning(pid) {
|
|
32
|
+
if (!pid) return false
|
|
33
|
+
try {
|
|
34
|
+
process.kill(pid, 0)
|
|
35
|
+
return true
|
|
36
|
+
} catch (err) {
|
|
37
|
+
return err.code === 'EPERM'
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function startBackground(args, target) {
|
|
42
|
+
mkdirSync(DIR, { recursive: true })
|
|
43
|
+
const paths = statePaths(target)
|
|
44
|
+
|
|
45
|
+
const existing = readPid(paths)
|
|
46
|
+
if (isRunning(existing)) return { alreadyRunning: true, pid: existing, paths }
|
|
47
|
+
|
|
48
|
+
const fd = openSync(paths.log, 'a')
|
|
49
|
+
const child = spawn(process.execPath, [ENTRY, ...args], {
|
|
50
|
+
detached: true,
|
|
51
|
+
stdio: ['ignore', fd, fd],
|
|
52
|
+
})
|
|
53
|
+
child.unref()
|
|
54
|
+
closeSync(fd)
|
|
55
|
+
|
|
56
|
+
writeFileSync(paths.pid, `${child.pid}\n`)
|
|
57
|
+
return { pid: child.pid, paths }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function shortenPath(path) {
|
|
61
|
+
const home = homedir()
|
|
62
|
+
return path.startsWith(home) ? `~${path.slice(home.length)}` : path
|
|
63
|
+
}
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ping } from './ping.js'
|
|
2
|
+
|
|
3
|
+
const colorEnabled = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR
|
|
4
|
+
const style = (code) => (text) => (colorEnabled ? `\x1b[${code}m${text}\x1b[0m` : String(text))
|
|
5
|
+
const dim = style('2')
|
|
6
|
+
const red = style('31')
|
|
7
|
+
const green = style('32')
|
|
8
|
+
|
|
9
|
+
export function formatDuration(ms) {
|
|
10
|
+
if (ms < 1000) return `${Math.round(ms)}ms`
|
|
11
|
+
if (ms < 10_000) return `${(ms / 1000).toFixed(1)}s`
|
|
12
|
+
return `${Math.round(ms / 1000)}s`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function formatEvery(ms) {
|
|
16
|
+
const seconds = ms / 1000
|
|
17
|
+
const trim = (n) => String(Math.round(n * 100) / 100)
|
|
18
|
+
if (seconds < 60) return `${trim(seconds)}s`
|
|
19
|
+
if (seconds < 3600) return `${trim(seconds / 60)}m`
|
|
20
|
+
return `${trim(seconds / 3600)}h`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function clock(date = new Date()) {
|
|
24
|
+
return date.toTimeString().slice(0, 8)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function sleep(ms) {
|
|
28
|
+
let cancel = () => {}
|
|
29
|
+
const promise = new Promise((resolve) => {
|
|
30
|
+
const timer = setTimeout(done, ms)
|
|
31
|
+
function done() {
|
|
32
|
+
clearTimeout(timer)
|
|
33
|
+
cancel = () => {}
|
|
34
|
+
resolve()
|
|
35
|
+
}
|
|
36
|
+
cancel = done
|
|
37
|
+
})
|
|
38
|
+
promise.cancel = () => cancel()
|
|
39
|
+
return promise
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function monitor(opts, target, version) {
|
|
43
|
+
const url = new URL(target.toString())
|
|
44
|
+
if (opts.path) url.pathname = opts.path
|
|
45
|
+
if (!url.pathname) url.pathname = '/'
|
|
46
|
+
url.hash = ''
|
|
47
|
+
|
|
48
|
+
const label = url.host + (url.pathname === '/' ? '' : url.pathname)
|
|
49
|
+
|
|
50
|
+
let stopped = false
|
|
51
|
+
const pendingSleeps = new Set()
|
|
52
|
+
|
|
53
|
+
const stop = () => {
|
|
54
|
+
if (stopped) return
|
|
55
|
+
stopped = true
|
|
56
|
+
for (const waiter of pendingSleeps) waiter.cancel()
|
|
57
|
+
pendingSleeps.clear()
|
|
58
|
+
if (!opts.quiet && !opts.once) console.log(dim('\nstopped'))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
process.on('SIGINT', stop)
|
|
62
|
+
process.on('SIGTERM', stop)
|
|
63
|
+
|
|
64
|
+
if (!opts.quiet && !opts.once) {
|
|
65
|
+
console.log(`${dim(`Keeping ${label} awake every ${formatEvery(opts.intervalMs)} · Ctrl+C to stop`)}`)
|
|
66
|
+
if (!url.hostname.endsWith('.onrender.com')) {
|
|
67
|
+
console.log(dim(`note: ${url.hostname} is not an .onrender.com app, pinging it anyway`))
|
|
68
|
+
}
|
|
69
|
+
console.log('')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let consecutiveFailures = 0
|
|
73
|
+
let lastResult = null
|
|
74
|
+
|
|
75
|
+
while (!stopped) {
|
|
76
|
+
const result = await ping(url, { timeoutMs: opts.timeoutMs })
|
|
77
|
+
lastResult = result
|
|
78
|
+
if (stopped) break
|
|
79
|
+
|
|
80
|
+
const time = dim(clock())
|
|
81
|
+
|
|
82
|
+
if (result.ok) {
|
|
83
|
+
const recovered = consecutiveFailures > 0 ? dim(' back up') : ''
|
|
84
|
+
consecutiveFailures = 0
|
|
85
|
+
if (!opts.quiet) {
|
|
86
|
+
console.log(`${time} ${green('✓')} ${result.status} ${formatDuration(result.ms)}${recovered}`)
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
consecutiveFailures += 1
|
|
90
|
+
const what = result.status
|
|
91
|
+
? `${result.status} ${formatDuration(result.ms)}`
|
|
92
|
+
: `no response${result.error ? ` (${result.error})` : ''}`
|
|
93
|
+
console.log(`${time} ${red('✗')} ${red(what)}`)
|
|
94
|
+
if (consecutiveFailures === 3) {
|
|
95
|
+
console.log(dim(' still down — it may be asleep, crashed or deploying'))
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (opts.once || stopped) break
|
|
100
|
+
|
|
101
|
+
const waiter = sleep(opts.intervalMs)
|
|
102
|
+
pendingSleeps.add(waiter)
|
|
103
|
+
await waiter
|
|
104
|
+
pendingSleeps.delete(waiter)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (opts.once) return lastResult?.ok ? 0 : 1
|
|
108
|
+
return 0
|
|
109
|
+
}
|
package/lib/ping.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const USER_AGENT = 'keepalive-render (+https://github.com/Aule-Labs/keepAlive)'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Ping a URL once. Never throws: network failures come back as { ok: false }.
|
|
5
|
+
* @returns {Promise<{ok: boolean, status: number|null, ms: number, error?: string}>}
|
|
6
|
+
*/
|
|
7
|
+
export async function ping(url, { timeoutMs = 30_000 } = {}) {
|
|
8
|
+
const started = Date.now()
|
|
9
|
+
const controller = new AbortController()
|
|
10
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const response = await fetch(url, {
|
|
14
|
+
method: 'GET',
|
|
15
|
+
redirect: 'follow',
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
headers: {
|
|
18
|
+
'user-agent': USER_AGENT,
|
|
19
|
+
accept: '*/*',
|
|
20
|
+
'cache-control': 'no-cache',
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// Drain the body so the connection closes cleanly and the server logs a full request.
|
|
25
|
+
await response.arrayBuffer().catch(() => {})
|
|
26
|
+
|
|
27
|
+
return { ok: response.ok, status: response.status, ms: Date.now() - started }
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
status: null,
|
|
32
|
+
ms: Date.now() - started,
|
|
33
|
+
error: describeError(error, timeoutMs),
|
|
34
|
+
}
|
|
35
|
+
} finally {
|
|
36
|
+
clearTimeout(timer)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function describeError(error, timeoutMs) {
|
|
41
|
+
if (error?.name === 'AbortError' || error?.name === 'TimeoutError') {
|
|
42
|
+
return `timed out after ${Math.round(timeoutMs / 1000)}s`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const cause = error?.cause?.code || error?.cause?.message
|
|
46
|
+
const code = error?.code
|
|
47
|
+
|
|
48
|
+
if (code === 'ENOTFOUND' || cause === 'ENOTFOUND') return 'DNS lookup failed'
|
|
49
|
+
if (code === 'ECONNREFUSED' || cause === 'ECONNREFUSED') return 'connection refused'
|
|
50
|
+
if (code === 'ECONNRESET' || cause === 'ECONNRESET') return 'connection reset'
|
|
51
|
+
|
|
52
|
+
return error?.cause?.message || error?.message || 'request failed'
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keepalive-render",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Keep free-tier Render services awake by pinging any .onrender.com URL every 10 minutes.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"keepalive-render": "bin/keepalive.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"lib",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "node bin/keepalive.js"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"render",
|
|
23
|
+
"render.com",
|
|
24
|
+
"onrender",
|
|
25
|
+
"keepalive",
|
|
26
|
+
"keep-alive",
|
|
27
|
+
"ping",
|
|
28
|
+
"free-tier",
|
|
29
|
+
"spin-down",
|
|
30
|
+
"cli",
|
|
31
|
+
"uptime"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/Aule-Labs/keepAlive.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/Aule-Labs/keepAlive/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/Aule-Labs/keepAlive#readme",
|
|
41
|
+
"author": "Aule Labs",
|
|
42
|
+
"license": "MIT"
|
|
43
|
+
}
|