ohmyohmyohmy
Hello all,
Usually when I write a program in another language like Python, there’s a clear-cut way to start something without having to manually execute a function from the command line (mix run -e Module.function()). That’s because, from the way I see it, there’s ‘main’ entry-point function from which the program launches, and other functions get run from there.
I can’t find any article that simply points to Phoenix’s main entry-point function from which I can simply execute a simple function (My.Stuff.populate_database()) without having to write some Agent/GenServer module and Supervising children? I get article’s like “What’s the main use of Phoenix”, but nothing that points to an entry-point of the program from which I can just run a function.
I need help. Where is Elixir/Phoenix’s ‘main’ function or its equivalent which which I can run a simple function? Thank you.
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
The
:phoenixapplication has it’s application start callback inPhoenix.https://github.com/phoenixframework/phoenix/blob/main/lib/phoenix.ex
But you can not really call anything from there, that code has already been written.
And even though you can reload modules at will, the application has probably already been started.
Maybe explain a bit better what you are trying to achieve.
ohmyohmyohmy
I am experimenting with Ecto and I have a module called Custom.DB that creates an in-memory database.
I have included the module as one of the children in application.ex.
However, before I can use it, I have to first create the tables, and populate the database. I’ve created a function for doing so, but I don’t know how to get it to run when the program starts with iex -S mix phx.server.
in application.ex:
Where do I run Custom.DB.create_tables()?
LostKobrakai
There’s no single entrypoint on the beam, which you can customize. That’s not how the abstractions are setup. The beam when starting will start a set of application. Each application can optionally provide a callback module, which (among others) will have the beam execute the
start/2function of that application. That’s where you put code to execute on startup.You’re already have that callback module and function, because that’s where the
childrenlist is defined, which you posted.ohmyohmyohmy
Mhmm… thank you for trying, but I still don’t get it. Maybe my question wasn’t specific enough.
My question is: Where do I run Custom.DB.create_tables() so my database is populated with whatever I have in ‘repo/testing.sql’ when I run iex -S mix phx.server?
NobbZ
That totally depends on your exact setup, though I would probably do that kind of seeding in the process that owns the database, such that it’s reseeded if the process ever has to be restarted.
LostKobrakai
Do you want to do that whenever your application starts, only in development, only when using the
phx.servermix task, …. There’s a lot of versions to this question, which do not share their answer.ohmyohmyohmy
I see. So there’s a lot more to this.
I want to have this run whenever my application starts. So, Custom.DB.create_tables() should be executed everytime I start the server, in dev or prod.
Once the database is populated, I can render views using the data within wih Ecto (Repo, Query, Schema).
LostKobrakai
Then the
start/2callback of your application module (usuallyMyApp.Application) is the place to run such things.NobbZ
I really think the owning process should be responsible, not the application, to ensure that a crash in the DB process at least recovers into “empty but usable” database, rather than a database that doesn’t even have a schema.
LostKobrakai
Maybe. Sometimes that additional level of rigor is worth it. But I’d argue that’s step two. It’s no longer just the question of where the beams “main” is.