FIX minimodifs
[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 % dossier utilisé lors de gros trie par qlc, voir qlc:keysort()
33 file:set_cwd("/tmp"),
34 loop().
35
36
37 loop() ->
38 % on attend une minute de plus pour prevenir une dérive nétive
39 timer:sleep(1000 * trunc(temps_prochaine_election() + 60)),
40 euphorik_bd:elire_troll(),
41 euphorik_daemon:loop().
42
43
44 % Renvoie le nombre de seconde qu'il reste jusque au prochain lundi à l'heure donnée (l'heure est sur 24heures)
45 % 86400 est le nombre de seconde dans un jour
46 temps_prochaine_election() ->
47 {Date, {H,M,S}} = calendar:local_time(),
48 Delta = (?JOUR_ELECTION_TROLL - 1) * 86400 + ?HEURE_ELECTION_TROLL * 60 * 60
49 -((calendar:day_of_the_week(Date) - 1) * 86400 + H * 60 * 60 + M * 60 + S),
50 % attention au cas où deux dates (maintenant et la date d'éction) ne se trouvent pas dans la même semaine.
51 if Delta =< 0 -> Delta + 7 * 86400; true -> Delta end.
52
53
54 % Recharge tous les modules euphorik.
55 % Appelé lors d'une mise en prod.
56 % TODO : récupérer les noms à partir des .beam dans /modules/ebin
57 reload_euphorik() ->
58 lists:foreach(
59 fun(M) ->
60 code:purge(M),
61 code:load_file(M)
62 end,
63 [euphorik_minichat_conversation, euphorik_protocole, euphorik_requests, euphorik_bd, euphorik_daemon]
64 ).
65
66