I'm loading modules dynamically on my raku rest/API application.
I want to avoid the server restarting each time I made any modification to my modules.
Is there a way to reload the raku modules each time you call require?
try require ::('Foo');
Cro does this. You can build your server on top of Cro, that's what I would recommend. Alternatively, you can study the Cro code to learn how it handles automatic restarts. Cro::Tools::Runner would be a good starting point methinks.
You have to take into account that all modules are precompiled before being loaded. What you are loading is not the source, but the CompUnit that has been generated from it, which is by default in a .precomp
directory. This precompilation takes also care of versions, for instance, but the main thing is that it's bytecode, not the source.
There's no easy workaround to that, other than taking in your hands that machinery (that is, checking if there are changes in the source, precompiling it, loading the precompiled binaries wherever they are and running them), so I think that, in this case, it's better to ask yourself if there's really an use case for this; maybe what you need to do is to speed up somehow server restart, maybe through automation using sake, or running integration tests from a git hook without the need to restart the server
You can't reload a module in a process. When a module identity is looked up it is cached, and looking up that identity a second time will continue to use the CompUnit that matched the first time. Precompilation has no bearing on this.
See:
https://github.com/rakudo/rakudo/blob/0b8ede6c459be752adef9cbbf7b59c961d9df0b0/src/core.c/CompUnit/Repository/Installation.pm6#L445-L453
https://github.com/rakudo/rakudo/blob/0b8ede6c459be752adef9cbbf7b59c961d9df0b0/src/core.c/CompUnit/Repository/FileSystem.pm6#L13-L21
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With