abhay831

abhay831

Delaying a Task in elixir

I am trying to show post to the first friend of a person first and other’s after making a delay of 1 mins,For that i am using genserver.

Problem is occurring that,the first friend as well as the other friends are getting the post after 1 min.

Here is my code of genserver:

defmodule Phoenix.SchedulePost do
	use GenServer
  
  def start_link(state) do
    GenServer.start_link __MODULE__,state
  end

  def init(state) do
  	schedule_post(state)
    {:ok, state}
  end
  #handling looby 
  def handle_info(:postSchedule,state) do
    #sending posts to others
    {:noreply,state}
  end
  #scheduling a task
  defp schedule_post(state) do
  	IO.puts "scheduling the task"
    Process.send_after(self(),:postSchedule,1*60*1000)
  end
end 

I am starting genserver process for each post request and sending it to the first friend,
Here is the code:

def handle_in("post:toFrstFrnd", %{"friendId"=>friendId,"body" => body}, socket) do
	newSocket = PhoenixWeb.SocketBucket.get_socket(friendId)
	if  newSocket != nil do
	  push newSocket, "post:toFrstFrnd", %{"friendId": friendId,"body": body}
	end
	Phoenix.SchedulePost.start_link(postId);
	 {:noreply, socket}
end

Help me out,Thank you in advance.

Marked As Solved

hubertlepicki

hubertlepicki

I think that is totally separated problem resulting from that you are starting one job to do these two different things. I think you should start two jobs/processes, or even process per friend and give it a delay as a paremeter that they’d use to sleep on to. For the one you want to get notification immediately, you just pass 0.

If you are going to keep your own solution, change the :postSchedule message to be an actual tuple, consisting of {:postSchedule, user_id} and schedule a number of these messages in the queue, with different third send_after parameter to send those out at different dates. This way you are also de-coupling the behavior so if something fails for one user, others would not be affected.

Also Liked

OvermindDL1

OvermindDL1

For note, a while back I’ve made an efficient (at least far more efficient than lots of sleeping tasks) library to handle this:

:slight_smile:

abhay831

abhay831

That’s a good work my friend,whether the taskAfter job will be async or it will make the handler to wait for the timeout.

hubertlepicki

hubertlepicki

There is one more thing you have to consider. If you do sleep tasks like that, and then you have a traditional deployment that does stop and start the Erlang VM during the process, your sleeping tasks would be silently killed and cancelled. It might be totally okay to do what you do, but in a scenario where you do multiple deployments per day it can be a show stopper as lots of these tasks would never be executed and your user experience will suffer.

So, if it’s the second case, I would advise storing the jobs somewhere. It does look like exq library already allows you to schedule delayed execution of tasks. I haven’t used it but it does appear to have enqueue_at function that wil ldo what you need: https://hexdocs.pm/exq/Exq.Enqueuer.html#enqueue_at/5

abhay831

abhay831

Thank you,it worked.

OvermindDL1

OvermindDL1

Yeah Tasks are in-system only. If it needs to persist I’d recommend a database or mnesia or dets or something.

Oooo, that’s an idea… I could add a (d)ets backend to TaskAfter as an option, can someone open up an issue for that feature? :slight_smile:

Maybe have pluggable backends via a simple behaviour, hmm…

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement