CharlesO
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
bryanjos
Maybe Code.require_file or Kernel.ParallelCompiler.files will do what you need?
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
CharlesO
Your suggestion of
Code.require_file/2works.