washeku

washeku

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?

Showing Posts 1 to 10

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.

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!

washeku

washeku OP

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?
how to make it terminate itself?

OvermindDL1

OvermindDL1

If a GenServer I’d do a send_after to send a kill message to self() after whatever N minutes, along with a handle that when it receives that message then it dies. :slight_smile:

In a GenServer I’d either have it set an internal timeout as all callbacks in a GenServer can take (except terminate for obvious reasons ^.^) and just have it keep bouncing or use send_after again to send a ‘time to poll’ message, where you poll and then call send_after again based on the response. :slight_smile:

Based on what condition? In a GenServer you just return {:stop, :normal} to do a normal non-error terminate. :slight_smile:

washeku

washeku OP

how to do that?

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
idi527

idi527

I think handle_info callback should return {:stop, :normal, state}.

OvermindDL1

OvermindDL1

Possibly, I was just going from memory so that is very likely and makes sense. ^.^

washeku

washeku OP

thanks, I’ll try that out.

washeku

washeku OP

on a status which returns some console application I’ve mentioned.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews