washeku

washeku

Background Task with autokilling itself and repeated action each N minutes

I want to create a bunch of background jobs dynamically. A job should run at most N minutes, strictly, and then kill itself.
It’ll check a state of some console application once a minute and if a state returns true, a job writes data into a database and kills itself ahead right away, otherwise it’ll keep checking for N minutes at most.

I’m looking for pointers of how to achieve that. I want a simple solution.
Should I use Task? If so, how should I specify the N minutes over the course of which it’ll be alive?
How can I make it to poll a state of an application once a minute?

Most Liked

josevalim

josevalim

Creator of Elixir

For some yes, for others no. :slight_smile: We need to be careful with those sentences because they may discourage conversation. If something is trivial and someone doesn’t understand it, they may think it is their fault after all.

In any case, you are right, so thanks for pointing to the right direction. @washeku please see the following example as a starting point: http://stackoverflow.com/questions/32085258/how-to-run-some-code-every-few-hours-in-phoenix-framework

if it is not clear, please ask! I am sure @OvermindDL1, myself and others will be happy to help!

OvermindDL1

OvermindDL1

All fairly trivial with a GenServer, you can even send back yourself a message with a timeout (it only arrives if no other messages have arrived) or always in some-odd minutes regardless of any other messages (send_after). :slight_smile:

You can do it with Task by using send_after though, but a GenServer really sounds best for this task as it does not sound like a one-off.

OvermindDL1

OvermindDL1

With send_after, something like:

def init(args) do
  killSelfTime = args.death_minutes * 1000 * 60 # send_after wants milliseconds
  Process.send_after(self(), :kill_self, killSelfTime)
  state = encodeWhateverYouWantToDoHere(args)
  {:ok, state}
end

def handle_info(:kill_self, state) do
  cleanup()
  {:stop, :normal}
end

defp cleanup() do
  # If you need to do something else like cancel some pending function or un-register with
  # another server or something
end

Where Next?

Popular in Questions Top

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
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
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 41989 114
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement