@@ -3,20 +3,15 @@ defmodule LambdaEthereumConsensus.Ticker do
3
3
4
4
use GenServer
5
5
6
+ @ tick_time 1000
7
+
6
8
require Logger
7
9
8
10
@ spec start_link ( [ atom ( ) ] ) :: :ignore | { :error , any } | { :ok , pid }
9
11
def start_link ( opts ) do
10
12
GenServer . start_link ( __MODULE__ , opts , name: __MODULE__ )
11
13
end
12
14
13
- @ spec register_to_tick ( atom ( ) | [ atom ( ) ] ) :: :ok
14
- def register_to_tick ( to_tick ) when is_atom ( to_tick ) , do: register_to_tick ( [ to_tick ] )
15
-
16
- def register_to_tick ( to_tick ) when is_list ( to_tick ) do
17
- GenServer . cast ( __MODULE__ , { :register_to_tick , to_tick } )
18
- end
19
-
20
15
##########################
21
16
### GenServer Callbacks
22
17
##########################
@@ -25,19 +20,13 @@ defmodule LambdaEthereumConsensus.Ticker do
25
20
@ spec init ( [ atom ( ) ] ) :: { :ok , [ atom ( ) ] } | { :stop , any }
26
21
def init ( to_tick ) when is_list ( to_tick ) do
27
22
schedule_next_tick ( )
28
-
29
23
{ :ok , to_tick }
30
24
end
31
25
32
- @ impl true
33
- def handle_cast ( { :register_to_tick , to_tick_additions } , to_tick ) do
34
- new_to_tick = Enum . uniq ( to_tick ++ to_tick_additions )
35
- { :noreply , new_to_tick }
36
- end
37
-
38
26
@ impl true
39
27
def handle_info ( :on_tick , to_tick ) do
40
28
schedule_next_tick ( )
29
+ # If @tick_time becomes less than 1000, we should use :millisecond instead.
41
30
time = :os . system_time ( :second )
42
31
43
32
# TODO: This assumes that on_tick/1 is implemented for all modules in to_tick
@@ -53,7 +42,7 @@ defmodule LambdaEthereumConsensus.Ticker do
53
42
54
43
def schedule_next_tick ( ) do
55
44
# For millisecond precision
56
- time_to_next_tick = 1000 - rem ( :os . system_time ( :millisecond ) , 1000 )
45
+ time_to_next_tick = @ tick_time - rem ( :os . system_time ( :millisecond ) , @ tick_time )
57
46
Process . send_after ( __MODULE__ , :on_tick , time_to_next_tick )
58
47
end
59
48
end
0 commit comments