Skip to content

Commit 37ff65f

Browse files
committed
force reload feature
1 parent 109fd3f commit 37ff65f

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ in both file types (dynamic served and services), you can use all node/bun metho
112112
- `service_require(service path)`: returns the matching service object
113113
- `service_require_try(service path)`: returns the matching service object or null if not found or if disabled
114114
- `rtjscomp`: has these properties/methods:
115-
- `actions`: an object with methods for server control (http[s]_[start|stop|kill], log_clear, halt, exit)
115+
- `actions`: an object with methods for server control (http[s]_[start|stop|kill], log_clear, file_watch_force_all, halt, exit)
116116
- `async data_load(path)`: reads the file in data directory and returns its content or null
117117
- `async data_load_watch(path, callback(content))`: executes callback first and on every change
118118
- `async data_save(path, content)`: writes the content to the file in data directory
119119

120+
most file updates are handled automatically, but some updates cannot be detected, for example by git, so you can force a reload of all files by calling `rtjscomp.actions.reload_force()` or sending a `SIGHUP` signal to the process (done by systemctl reload).
121+
120122
## environment variables
121123
- `rtjscomp_path_data`: path to data directory, default is `./data`
122124
- `rtjscomp_path_public`: path to public directory, default is `./public`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rtjscomp",
3-
"version": "0.11.2",
3+
"version": "0.12.0",
44
"description": "php-like server but with javascript",
55
"repository": {
66
"type": "git",

rtjscomp.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ const type_raws = new Set([
139139
'zip',
140140
]);
141141

142-
/// compiled file handlers
142+
/// state
143143
const file_cache_functions = new Map;
144+
const watch_callbacks = new Set;
144145

145146
const actions = {};
146147
const rtjscomp = global.rtjscomp = {
@@ -200,6 +201,21 @@ rtjscomp.escape_html = str => (
200201
: str
201202
);
202203

204+
const file_watch = (path, callback) => {
205+
watch_callbacks.add(callback);
206+
const watcher = fs.watch(path, WATCH_OPTIONS, callback);
207+
return {
208+
close: () => {
209+
watch_callbacks.delete(callback);
210+
watcher.close();
211+
},
212+
};
213+
};
214+
actions.reload_force = () => {
215+
log('force file reload');
216+
for (const callback of watch_callbacks) callback();
217+
}
218+
203219
/**
204220
hack to guess the line number of an error
205221
*/
@@ -370,7 +386,7 @@ const service_update = async service_object => {
370386
if (service_object.status !== SERVICE_STATUS_PENDING) return;
371387
if (!service_object.watcher) {
372388
let timeout = 0;
373-
service_object.watcher = fs.watch(path_real, WATCH_OPTIONS, () => (
389+
service_object.watcher = file_watch(path_real, () => (
374390
clearTimeout(timeout),
375391
timeout = setTimeout(() => (
376392
log_verbose && log('file updated: ' + path),
@@ -605,7 +621,7 @@ const service_require_try = path => {
605621
}
606622

607623
const file_watch_once = (path, callback) => {
608-
const watcher = fs.watch(path, WATCH_OPTIONS, () => (
624+
const watcher = file_watch(path, () => (
609625
watcher.close(),
610626
log_verbose && log('file updated: ' + path),
611627
callback()
@@ -623,7 +639,7 @@ const file_keep_new = async (path, callback) => {
623639
}
624640

625641
let timeout = 0;
626-
return fs.watch(path, WATCH_OPTIONS, () => (
642+
return file_watch(path, () => (
627643
clearTimeout(timeout),
628644
timeout = setTimeout(() => exiting || (
629645
log_verbose && log('file updated: ' + path),
@@ -1706,6 +1722,7 @@ process.on('SIGINT', actions.exit);
17061722
//process.on('SIGUSR1', actions.exit);
17071723
process.on('SIGUSR2', actions.exit);
17081724
process.on('SIGTERM', actions.exit);
1725+
process.on('SIGHUP', actions.reload_force);
17091726

17101727
log(`rtjscomp v${
17111728
VERSION

0 commit comments

Comments
 (0)