erste vorarbeiten für JSON
[aymargeddon/current.git] / src / FROGS / scheduler.pl
1 #!/usr/bin/perl -w
2 #########################################################################
3 #
4 #   Copyright (c) 2003 Aymargeddon Development Team
5 #
6 #   This file is part of
7 #   "FROGS" = Framework for Realtime Online Games of Strategy
8 #
9 #   FROGS is free software; you can redistribute it and/or modify it
10 #   under the terms of the GNU General Public License as published by the Free
11 #   Software Foundation; either version 2 of the License, or (at your option)
12 #   any later version.
13 #
14 #   FROGS is distributed in the hope that it will be useful, but WITHOUT
15 #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 #   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17 #   more details.
18 #   You should have received a copy of the GNU General Public License along
19 #   with this program; if not, write to the Free Software Foundation, Inc., 675
20 #   Mass Ave, Cambridge, MA 02139, USA.
21 #
22 ###########################################################################
23 #
24 #  scheduler daemon
25 #
26 # usage:
27 # ./scheduler.pl [conf-parameter]=[value] ...
28 # example:
29 # ./scheduler.pl -DURATION-PRAY=1000 -DURATION-PRODUCE=1000
30
31 use strict;
32 use lib qw(..);
33 use FROGS::DataBase;
34 use FROGS::Command;
35 use FROGS::Config qw ($conf);
36 use Data::Dumper;
37
38 $|=1;
39
40 Util::open_log();
41
42 # read command line
43 Util::overwrite_config(@ARGV);
44
45 my $KEYCOL="SUBMIT";
46
47 my $db= new DataBase;
48 # $db->nowrite();
49 require $::conf->{-COMMANDS};
50 my $now = $db->now();
51 Util::log("\n$now Scheduler started...",0);
52 loop();
53 exit;
54
55 sub loop {
56   #     read next using our db connection (complicated due to poor SQL
57   #     implemention of MYSQL :-/
58   my $nulldate = $db->quote('0000-00-00 00:00:00'); # TODO: ugly and unportable :-(
59   my $nullcond = "(DONE IS NULL OR DONE=$nulldate)";
60   my $count = 0;
61   while (1) {
62     $count++;
63     $db->commit(); # ??? WHY IS THIS NECESSARY TO SEE COMMANDS FROM CLIENTS ???
64     my ($minexec) = $db ->single_select("SELECT min( exec ) FROM COMMAND ".
65                                         "WHERE $nullcond and EXEC <= NOW()");
66     $minexec = $db ->quote($minexec);
67     my ($id) = $db ->single_select("SELECT min(id) FROM COMMAND ".
68                                    "WHERE $nullcond and EXEC = $minexec");
69     $id= $db ->quote($id);
70     my $command_entry =  $db ->single_hash_select("COMMAND",
71                                                   "EXEC = $minexec and ID = $id ");
72
73     # delete outdated events
74     $db->delete_from('EVENT','TIME < NOW()') if $::conf->{-DELETE_OLD_EVENTS};
75
76     if (! defined  $command_entry ) {
77       # sleep if no command in db
78       sleep ($::conf->{-SCHEDULER_SLEEP});
79       Util::log('.',-1);
80
81       if($count % $::conf->{-LOG_TIME_IN_LOOP} == 0){
82         my $now = $db->now();
83         Util::log("\n###\n### $now: counted $count loops\n###\n",1);
84       }
85       next;
86     }
87
88     #   create command object
89     my $command = $command_entry->{"COMMAND"}->new($command_entry,$db);
90     # execute command
91     if($command){
92       $command->execute();
93       $db->commit;
94     }
95   }
96 };
97