ohmyohmyohmy

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.

Showing Posts 1 to 10

NobbZ

NobbZ

The :phoenix application has it’s application start callback in Phoenix.

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

ohmyohmyohmy OP

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.

defmodule Custom.DB do
  use Ecto.Repo,
    otp_app: :testing,
    adapter: Ecto.Adapters.SQLite3

  def create_tables do
    :testing
    |> :code.priv_dir()
    |> Path.join("repo/testing.sql")
    |> File.read!()
    |> query()
  end

  def show_tables do
    query("select * from sqlite_schema;")
  end
end

in application.ex:

    children = [
    ...

      {Custom.DB, database: :memory, pool_size: 1}
      ...
    ]

Where do I run Custom.DB.create_tables()?

LostKobrakai

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/2 function 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 children list is defined, which you posted.

ohmyohmyohmy

ohmyohmyohmy OP

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

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

LostKobrakai

Do you want to do that whenever your application starts, only in development, only when using the phx.server mix task, …. There’s a lot of versions to this question, which do not share their answer.

ohmyohmyohmy

ohmyohmyohmy OP

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

LostKobrakai

Then the start/2 callback of your application module (usually MyApp.Application) is the place to run such things.

NobbZ

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

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.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews