CharlesO
Dynamically compile and load modules
Is it possible to have Elixir modules compiled and loaded at run time dynamically?
For example, say I have Elixir modules with custom logic for specific customers.
I need to load / call functions from these modules at runtime based on inbound requests.
What i have tried:
- define a path for ALL custom modules in Application.start
Code.append_path(Application.get_env(:bothost, :modules))
-
check if a given module is loaded; if not, compile/ load it, then call an expected entry point function on the module:
defp process([{, %BotHost.BotInfo{module: module} = bot}], req) when module == nil, do: BotHost.Processor.process(bot, req) # use generic processor
defp process([{, %BotHost.BotInfo{module: module} = bot}], req) do
case Code.ensure_compiled(module) do
{:module, _module} → apply(module, :process, [bot, req])
{:error, err} → log “load failed: #{module} #{err}” ; BotHost.Processor.process(bot, req) # unable to load custom processor, use generic processor instead
end
end
Please can we use the functions from the “Code” module for this purpose?
Marked As Solved
bryanjos
Also Liked
CharlesO
Your suggestion of Code.require_file/2 works.
defp process([{_, %BotHost.BotInfo{module: module} = bot}], req) when module == nil, do: BotHost.Processor.process(bot, req) # use generic processor
defp process([{_, %BotHost.BotInfo{module: module, module_file: file} = bot}], req) do
case Code.require_file(file, "modules") do
nil -> apply(module, :process, [bot, req])
[{module, _}] -> apply(module, :process, [bot, req])
err -> log "load failed: #{module} - #{inspect err}" ; BotHost.Processor.process(bot, req) # unable to load bot-specific module, use generic processor instead
end
end
CharlesO
Thanks for the reply. I’ll keep investigating the options.
Right now i’m looking at this:
to understand what others have tried. Unfortunately there seems to be very little information on what i’m trying to do out there
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









