fireproofsocks

fireproofsocks

Running code on Phoenix start?

This isn’t a Phoenix question per se, it’s more of a general Elixir how to, but I would like to run some extra code when I first start up my Phoenix application. Specifically, I would like to warm up the cache with entries that are persisted in the database (e.g. session data). There might also be some ad hoc things to do on startup like ping a web URL for notification purposes etc. Where is a valid place to put code like that? Should that all go inside the Application start/2 function?

Thanks for any tips!

Most Liked

fireproofsocks

fireproofsocks

In my case, sessions are in the database for analytics and persistence purposes, but from the point of view of the front-end, the only place that gets queried is in cache. It would be possible to have a fallback mechanism that would fall-back to a database lookup on cache miss, but I want to avoid doing that because doing that would open up some vulnerabilities to overloading the database.

Perhaps something like this would have the desired effect:

defmodule MyApp.Application do
  alias MyApp.Contexts.MyContext
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    :ok = MyContext.warm_up_cache()
    
    children = [
      supervisor(MyApp.Repo, []),
    ]
    
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
sdr

sdr

Application.start_phase looks like another option.

Docs are a little light but the Erlang docs go into greater detail.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement