stratacast
Best way to name permanent tasks?
I’m trying to name a supervised, permanent task so I can find it easier. What’s the best way to go about this? I tried out this right here:
However, when I run Process.whereis(MyApplication.MyTask) instead of getting a PID back, I get nil. This is an ongoing looping task. From MyApplication.Application I also tried moving the starting of the Task via a different Supervisor call like so:
Supervisor.start_link([
{MyTask, arg}
], strategy: :one_for_one, id: :"MyApplication.MyTask")
And that didn’t seem to work either. I don’t know what exactly the :id would do, but I thought it was worth trying to pass that along.
Most Liked
benwilson512
Tasks are really not the right kind of thing to have running for a long time, you should use a genserver instead.
However to your specific question, id is not the value used to set the name, you need a name option, but I’m not even sure if tasks take a name option.
NobbZ
Indeed, Task.start_link/3 does not allow for something like the :name option.
ityonemo
Process.register/2 should work. But yeah. At the point where you have a stateless datastructure receiving messages inside a task, it’s time to use gen_statem. If you prefer a genserver like interface and enforced state graph on top of gen_statem, I wrote StateServer library for this purpose.
Otoh, if you’re doing this for fun and exploration of otp I highly encourage it!!
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









