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
For some yes, for others no.
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
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). ![]()
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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









