Creating a task by a user of my website

Creating multiple tasks in GenSever and terminating them

I have a webapplication. When a user clicks a button, I need to create a new task which would work for about 1 hour - 2hours in background. No heavy computation, just sending some http request around every 30 seconds to a remote server and parsing a response.

When a task receives a special response from a remote server, it should stop/kill itself. That’s it.

There can be many users clicking buttons at the same time and therefore many such jobs.

I’m new to GenServer. How can I use it to achieve my goal? Any recommendations on what I have to pay attention to?

You’ll probably want to supervise your tasks and register them in a Registry.
I recommend you take a look at this: Learning Supervisor basics

1 Like

Yep, definitely a GenServer as you surmised.

Uhh, do you want to do something to ensure they cannot overload your system? Is there a limit of 1 per logged on user? Is there anything to prevent someone from making a million users to spawn it a million times?

If you don’t care about throttling it is pretty easy. Just a standard GenServer, but have it broadcast a message to itself (send_after) every so often to do the http request.

1 Like

Only one task per user during a day.

Honestly I’d probably just make a single genserver instead of spawning one per user, just have it iterating an internal list, sleeping until the next time it has a pending operation or it receives a message, then when it is time then just do the request and repeat…

Thereoretically, a user can click on a button once a day or even more often. However, on practise it’ll happen once in a week or month. Thus, I’ll still need only a single GenServer?

Yeah honestly I’d still use only a single one, no point for more since it is basically acting as a scheduler. Only time I’d make more is if there were a TON of users all doing it, in which case I’d start sharding it across workers.

1 Like