Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 91bbaadd

Von Moritz Bunkus vor etwa 12 Jahren hinzugefügt

  • ID 91bbaadd9be01ec1401a953147119791da7daa9e
  • Vorgänger bfe2163f
  • Nachfolger 804893ae

Implementation eines programmatischen Interfaces zum Task-Server

Unterschiede anzeigen:

SL/System/TaskServer.pm
1
package SL::System::TaskServer;
2

  
3
use strict;
4

  
5
use parent qw(Rose::Object);
6

  
7
use Rose::Object::MakeMethods::Generic (
8
  scalar => [ qw(last_command_output) ],
9
);
10

  
11
use File::Slurp;
12
use File::Spec::Functions qw(:ALL);
13

  
14
use SL::System::Process;
15

  
16
use constant {
17
  OK           =>  0,
18
  ERR_PID_FILE => -1,
19
  ERR_PROCESS  => -2,
20
};
21

  
22
sub status {
23
  my ($self) = @_;
24

  
25
  my $pid = $self->_read_pid;
26
  return ERR_PID_FILE unless $pid;
27

  
28
  return kill(0, $pid) ? OK : ERR_PROCESS;
29
}
30

  
31
sub is_running {
32
  my ($self) = @_;
33

  
34
  return $self->status == OK;
35
}
36

  
37
sub start {
38
  my ($self) = @_;
39

  
40
  return $self->_run_script_command('start');
41
}
42

  
43
sub stop {
44
  my ($self) = @_;
45

  
46
  return $self->_run_script_command('stop');
47
}
48

  
49
sub wake_up {
50
  my ($self) = @_;
51

  
52
  my $pid = $self->_read_pid;
53
  return undef unless $pid;
54
  return kill('ALRM', $pid) ? 1 : undef;
55
}
56

  
57
#
58
# private methods
59
#
60

  
61
sub _read_pid {
62
  my ($self) = @_;
63

  
64
  my $exe_dir       = SL::System::Process->exe_dir;
65
  my $pid_file_name = join '.', splitdir($exe_dir), 'config.lx_office.conf.pid';
66
  my $pid_file_path = catfile(catdir($exe_dir, 'users', 'pid'), $pid_file_name);
67

  
68
  return undef unless -f $pid_file_path;
69
  return join('', read_file($pid_file_path)) * 1;
70
}
71

  
72
sub _run_script_command {
73
  my ($self, $command) = @_;
74

  
75
  my $exe = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
76
  $self->last_command_output(`${exe} ${command}`);
77

  
78
  return $? == 0 ? 1 : undef;
79
}
80

  
81
1;
82
__END__
83

  
84
=pod
85

  
86
=encoding utf8
87

  
88
=head1 NAME
89

  
90
SL::System::TaskServer - programmatic interface to the external task server component
91

  
92
=head1 SYNOPSIS
93

  
94
  # Create interface
95
  my $task_server = SL->TaskServer->new;
96

  
97
  # Start the server if it is not running
98
  if (!$task_server->is_running) {
99
    $task_server->start;
100
  }
101

  
102
  # Stop it if it is running
103
  if ($task_server->is_running) {
104
    $task_server->stop;
105
  }
106

  
107
=head1 FUNCTIONS
108

  
109
=over 4
110

  
111
=item C<is_running>
112

  
113
Returns C<trueish> if the server is running. This is done by using
114
Perl's C<kill> function with a "signal" of C<0> for the process ID
115
which in turn is read from the daemon's PID file.
116

  
117
If the PID file is not found or if C<kill> returns a non-successful
118
value then a C<falsish> value is returned.
119

  
120
=item C<last_command_output>
121

  
122
Returns the output of the last C<system> command executed, e.g. from a
123
call to L<start> or L<stop>.
124

  
125
=item C<start>
126

  
127
Starts the task server. Does not check whether or not it is running,
128
neither before not after trying to start it.
129

  
130
Returns C<1> if the system command C<./scripts/task_server.pl start>
131
exits with an exit code of C<0> and C<undef> otherwise.
132

  
133
The command's output can be queried with L<last_command_output>.
134

  
135
=item C<status>
136

  
137
Queries the task server status. Returns one of these values:
138

  
139
=over 4
140

  
141
=item *
142

  
143
C<OK> or C<0>: the task server is running and signals can be sent to
144
it.
145

  
146
=item *
147

  
148
C<ERR_PID_FILE> or C<-1>: the PID file could not be found or read
149

  
150
=item *
151

  
152
C<ERR_PROCESS> or C<-2>: the PID file could was found and read, but
153
it's not possible to send signals to the process, e.g. because it is
154
not running or owned by a different user ID.
155

  
156
=back
157

  
158
=item C<stop>
159

  
160
Stops the task server. Does not check whether or not it is running,
161
neither before not after trying to start it.
162

  
163
Returns C<1> if the system command C<./scripts/task_server.pl stop>
164
exits with an exit code of C<0> and C<undef> otherwise.
165

  
166
The command's output can be queried with L<last_command_output>.
167

  
168
=item C<wake_up>
169

  
170
Sends a signal to the task server process causing it to wake up and
171
process its job queue immediately.
172

  
173
Returns C<1> if the signal could be sent and C<undef> otherwise.
174

  
175
=back
176

  
177
=head1 BUGS
178

  
179
Nothing here yet.
180

  
181
=head1 AUTHOR
182

  
183
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
184

  
185
=cut

Auch abrufbar als: Unified diff