PLOP
[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 % - rechargement des modules lors d'une mise en production
22 % Date : 05.11.2007
23 % @author G.Burri
24
25
26 -module(euphorik_daemon).
27 -export([start/1, reload_euphorik/0, loop/0]).
28 -include("../include/euphorik_defines.hrl").
29
30
31 % Démarre le démon
32 start(_A) ->
33 loop().
34
35
36 loop() ->
37 % on attend une minute de plus pour prevenir une dérive négative
38 timer:sleep(1000 * (trunc(temps_prochaine_election() + 60))),
39 euphorik_bd:elire_troll(),
40 euphorik_daemon:loop().
41
42
43 % Renvoie le nombre de seconde qu'il reste jusque au prochain lundi à l'heure donnée (l'heure est sur 24heures)
44 % 86400 est le nombre de seconde dans un jour
45 temps_prochaine_election() ->
46 {Date, {H,M,S}} = calendar:local_time(),
47 Delta = (?JOUR_ELECTION_TROLL - 1) * 86400 + ?HEURE_ELECTION_TROLL * 60 * 60
48 -((calendar:day_of_the_week(Date) - 1) * 86400 + H * 60 * 60 + M * 60 + S),
49 % attention au cas où deux dates (maintenant et la date d'élection) ne se trouvent pas dans la même semaine.
50 if Delta =< 0 -> Delta + 7 * 86400; true -> Delta end.
51
52
53 % Recharge tous les modules euphorik.
54 % Appelé lors d'une mise en prod.
55 % TODO : récupérer les noms à partir des .beam dans /modules/ebin
56 reload_euphorik() ->
57 lists:foreach(
58 fun(M) ->
59 code:purge(M),
60 code:load_file(M)
61 end,
62 [
63 euphorik_common,
64 euphorik_minichat_conversation,
65 euphorik_protocole,
66 euphorik_requests,
67 euphorik_bd,
68 euphorik_bd_update,
69 euphorik_daemon
70 ]
71 ).
72