Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload raku modules at runtime, for debug purposes?

Tags:

raku

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');
like image 476
David Romero Avatar asked Nov 06 '19 00:11

David Romero


3 Answers

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.

like image 189
Holli Avatar answered Nov 01 '22 06:11

Holli


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

like image 4
jjmerelo Avatar answered Nov 01 '22 08:11

jjmerelo


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

like image 3
ugexe Avatar answered Nov 01 '22 08:11

ugexe