Daemon für Hintergrundjobs
[kivitendo-erp.git] / scripts / task_server.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
8 }
9
10 use CGI qw( -no_xhtml);
11 use Config::Std;
12 use Cwd;
13 use Daemon::Generic;
14 use Data::Dumper;
15 use DateTime;
16 use English qw(-no_match_vars);
17 use SL::Auth;
18 use SL::DB::BackgroundJob;
19 use SL::BackgroundJob::ALL;
20 use SL::Form;
21 use SL::Helper::DateTime;
22 use SL::LXDebug;
23 use SL::Locale;
24
25 my %config;
26
27 # this is a cleaned up version of am.pl
28 # it lacks redirection, some html setup and most of the authentication process.
29 # it is assumed that anyone with physical access and execution rights on this script
30 # won't be hindered by authentication anyway.
31 sub lxinit {
32   my $login = $config{task_server}->{login};
33
34   package main;
35
36   { no warnings 'once';
37     $::userspath  = "users";
38     $::templates  = "templates";
39     $::sendmail   = "| /usr/sbin/sendmail -t";
40   }
41
42   eval { require "config/lx-erp.conf";       1; } or die $EVAL_ERROR;
43   eval { require "config/lx-erp-local.conf"; 1; } or die $EVAL_ERROR if -f "config/lx-erp-local.conf";
44
45   $::lxdebug = LXDebug->new;
46   $::locale  = Locale->new($::language);
47   $::cgi     = CGI->new qw();
48   $::form    = Form->new;
49   $::auth    = SL::Auth->new;
50
51   die 'cannot reach auth db'               unless $::auth->session_tables_present;
52
53   $::auth->restore_session;
54
55   require "bin/mozilla/common.pl";
56
57   die "cannot find user $login"            unless %::myconfig = $::auth->read_user($login);
58   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
59 }
60
61 sub gd_preconfig {
62   my $self = shift;
63
64   read_config $self->{configfile} => %config;
65
66   die "Missing section [task_server] in config file"                unless $config{task_server};
67   die "Missing key 'login' in section [task_server] in config file" unless $config{task_server}->{login};
68
69   lxinit();
70
71   return ();
72 }
73
74 sub gd_run {
75   while (1) {
76     my $ok = eval {
77       $::lxdebug->message(0, "Retrieving jobs") if $config{task_server}->{debug};
78
79       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
80
81       $::lxdebug->message(0, "  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if $config{task_server}->{debug} && @{ $jobs };
82
83       $_->run for @{ $jobs };
84
85       1;
86     };
87
88     if ($config{task_server}->{debug}) {
89       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
90       $::lxdebug->message(0, "Sleeping");
91     }
92
93     my $seconds = 60 - (localtime)[0];
94     sleep($seconds < 30 ? $seconds + 60 : $seconds);
95   }
96 }
97
98 my $cwd     = getcwd();
99 my $pidbase = "${cwd}/users/pid";
100
101 mkdir($pidbase) if !-d $pidbase;
102
103 newdaemon(configfile => "${cwd}/config/task_server.conf",
104           progname   => 'lx-office-task-server',
105           pidbase    => "${pidbase}/",
106           );
107
108 1;