MOD mini modifications
[euphorik.git] / modules / erl / euphorik_daemon.erl
1 % coding: utf-8
2 % Copyright 2008 Grégory Burri
3 %
4 % This file is part of Euphorik.
5 %
6 % Euphorik is free software: you can redistribute it and/or modify
7 % it under the terms of the GNU General Public License as published by
8 % the Free Software Foundation, either version 3 of the License, or
9 % (at your option) any later version.
10 %
11 % Euphorik is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % You should have received a copy of the GNU General Public License
17 % along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18 %
19 % Module tournant en background s'occupant periodiquement de certaines tâches :
20 % - sélection du prochain troll chaque semaine
21 % Date : 05.11.2007
22 % @author G.Burri
23
24
25 -module(euphorik_daemon).
26 -export([start/1, reload_euphorik/0]).
27 -include("../include/euphorik_defines.hrl").
28
29
30 % Démarre le démon
31 start(_A) ->
32 loop().
33
34
35 loop() ->
36 % on attend une minute de plus pour prevenir une dérive négative
37 timer:sleep(1000 * trunc(temps_prochaine_election() + 60)),
38 euphorik_bd:elire_troll(),
39 euphorik_daemon:loop().
40
41
42 % Renvoie le nombre de seconde qu'il reste jusque au prochain lundi à l'heure donnée (l'heure est sur 24heures)
43 % 86400 est le nombre de seconde dans un jour
44 temps_prochaine_election() ->
45 {Date, {H,M,S}} = calendar:local_time(),
46 Delta = (?JOUR_ELECTION_TROLL - 1) * 86400 + ?HEURE_ELECTION_TROLL * 60 * 60
47 -((calendar:day_of_the_week(Date) - 1) * 86400 + H * 60 * 60 + M * 60 + S),
48 % attention au cas où deux dates (maintenant et la date d'éction) ne se trouvent pas dans la même semaine.
49 if Delta =< 0 -> Delta + 7 * 86400; true -> Delta end.
50
51
52 % Recharge tous les modules euphorik.
53 % Appelé lors d'une mise en prod.
54 % TODO : récupérer les noms à partir des .beam dans /modules/ebin
55 reload_euphorik() ->
56 lists:foreach(
57 fun(M) ->
58 code:purge(M),
59 code:load_file(M)
60 end,
61 [euphorik_common, euphorik_minichat_conversation, euphorik_protocole, euphorik_requests, euphorik_bd, euphorik_daemon]
62 ).
63