I have a system that creates a Question
and inserts this question into the database, I do this by clicking a link that I've set up. This is straightforward Phoenix stuff. Create a controller action setup a link for it and then click the button to fire that action.
This works for now. But the next phase is to have the system create a question without any intervention from the UI. So, that leads me to a new place with Elixir/Phoenix. My new problem is: I need to run this function automatically at x time of day.
Question:
What is the most idiomatic way of implementing a background task in Elixir/Phoenix? I know very little about Genserver or Supervisors but I think I'm ready to start learning about these tools. With all that being said, how would you approach a the problem of moving logic to a background job.
CODE:
def build_question(conn, _params) do
case Questions.create_total_points_question(conn) do
{:ok, _question} ->
conn
|> put_flash(:info, "Question created successfully.")
|> redirect(to: page_path(conn, :index))
{:error, _msg} ->
conn
|> redirect(to: page_path(conn, :index))
end
end
This controller action is triggered from a link. This code needs to be called in the background. Thanks for the help in advance.
Task.async/1
orKernel.spawn/1
? - Aleksei Matiushkin