Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to watch config for changes #207

Merged
merged 8 commits into from
Sep 1, 2020
Merged

Conversation

Half-Shot
Copy link
Contributor

This allows bridge developers to listen for config changes, which can be bubbled up to the bridge logic to support small changes without restarting the whole bridge. This is useful for things like the irc bridge, where restarts are costly.

I've also added some minimal Cli tests to ensure that config behaviour continues to work as expected.

@Half-Shot Half-Shot requested a review from a team August 23, 2020 11:52
@dalcde
Copy link
Contributor

dalcde commented Aug 23, 2020

I was thinking of a different workflow here, but I don't have strong opinions. The way, say, apache and nginx do this is that after you change the config, you send a SIGHUP signal to the process, instead of modifying the file directly.

This would interact appropriately with systemctl reload and can let people decide when they want to actually reload the config file (e.g. they might want to regenerate the registration file or do something else first). It's also helpful for people who want to edit config on a live instance (e.g. on a staging server) and want to be able to save it halfway through edits because of muscle memory.

@Half-Shot
Copy link
Contributor Author

@dalcde Great idea. I'll definitely update the code to use signals.

dalcde added a commit to dalcde/matrix-appservice-mattermost that referenced this pull request Aug 24, 2020
This will not be functional until some version of
matrix-org/matrix-appservice-bridge#207
is merged, since there is currently no way to access, load and validate
the new config file without reimplementing the logic of
matrix-appservice-bridge.
@jaller94 jaller94 removed the request for review from a team August 24, 2020 07:07
@Half-Shot Half-Shot requested a review from jaller94 August 24, 2020 09:01
@Half-Shot
Copy link
Contributor Author

This has been done.

const newConfig = this.loadConfig(configFilename);
// onConfigChanged is checked above, but for Typescript's sake.
if (this.opts.onConfigChanged) {
this.opts.onConfigChanged(newConfig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to update the this.bridgeConfig variable here?

In real life, only some config variables would be hot-reloadable. I would expect that if the new config edits other variables, the bridge should reject the new config file and retain the old one. We can make onConfigChanged return a boolean to indicate whether the new config file is accepted, and act based on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I don't want to update the config from underneath bridges because it could cause problems. For instance, certain bits of the IRC bridge cannot be reloaded so we wouldn't want to change those variables.

It will be up to the bridge developer to listen for the callback, and explicitly apply changes to the config inside the bridge. It might be a bit more faff, but it's safer.

@dalcde
Copy link
Contributor

dalcde commented Aug 24, 2020 via email

Copy link
Contributor

@jaller94 jaller94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Also great to see tests covering this reload functionality.

Off-topic:
I wonder if we should deep-copy or deep-freeze the opts parameter, because it seems like you expect it to be immutable.

Comment on lines 244 to 245
// onConfigChanged is checked above, but for Typescript's sake.
if (this.opts.onConfigChanged) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just for TypeScript's sake. We're copying the opts object by reference.
Any code in this class or the bridge could have re-set the value of opts.onConfigChanged before SIGHUP was received.

@Half-Shot Half-Shot merged commit 8b920c1 into develop Sep 1, 2020
@jaller94 jaller94 deleted the hs/watch-config branch September 1, 2020 16:02
@Half-Shot Half-Shot mentioned this pull request Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants