jeroenhouben

jeroenhouben

Run function on startup

Simple question I could not find an answer for…

I want to perform a HTTP request once at startup of my app and log the output. Phoenix app in my case but the question is general really. I don’t need a whole genserver and all, just a function to run… somewhere

Where do I put this code?

Marked As Solved

hauleth

hauleth

Add one of task in your supervisor children list. You can use Task.child_spec/1 for that. This is what I am using for such jobs like Slack notifications or systemd notifications.

Also Liked

entone

entone

You can also use the :ignore return value in a GenServer init callback in your supervision tree, that will run your init callback and then kill/ignore the process after it’s run.

Last Post!

ityonemo

ityonemo

defmodule Foo do 

  def go do
    GenServer.start_link(__MODULE__, :ok)
  end

  def init(_) do
    :ignore
  end

end

defmodule Bar do 

  def go do
    GenServer.start_link(__MODULE__, :ok)
  end

  def init(_) do
    {:stop, :reason}
  end

end

defmodule Baz do 

  def go do
    GenServer.start_link(__MODULE__, :ok)
  end

  def init(_) do
    {:stop, :normal}
  end

end

defmodule Quux do

  def go do
    Task.start_link(fn -> :do_something end)
  end

end

try them all in the console to get a feel of what the supervisors see.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement