I want to start a gen_server that additionally, will perform one action every minute.
What is the best way to schedule that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have two easy alternatives, use
timer:send_interval/2orerlang:send_after/3.send_intervalis easier to setup, whilesend_after(when used in the Erlang module) is more reliable since it is a built-in function, see the Efficiency Guide.Using
send_afteralso ensures that thegen_serverprocess is not overloaded. If you were using thesend_intervalfunction you would get a message regardless if the process can keep up or not. Withsend_afterbeing called just before the return inhandle_infoyou only schedule a new message once you handled the previous one. If you want more accurate time tracking you can still schedule asend_afterwith the time set dynamically to something lower than?INTERVAL(or even 0) to catch up.I would recommend something along the following lines in your
gen_server:Instead of
triggeryou could send something with a state if it is needed, like{trigger, Count}or something.