sub
status {
my
(
$self
) =
@_
;
my
@max
= ( 0, 1, 1, 1, 1, 1, 1 );
foreach
my
$job
(
$self
->driver->get_jobs ) {
my
$lasterror_msg
=
$job
->lasterror_msg;
$max
[0] =
length
$job
->name
if
$max
[0] <
length
$job
->name;
$max
[1] =
length
$job
->min_processes
if
$max
[1] <
length
$job
->min_processes;
$max
[2] =
length
$job
->max_processes
if
$max
[2] <
length
$job
->max_processes;
$max
[3] =
length
$job
->count_processes
if
$max
[3] <
length
$job
->count_processes;
$max
[4] =
length
$job
->lastrun
if
$max
[4] <
length
$job
->lastrun;
$max
[5] =
length
$job
->lasterror
if
$max
[5] <
length
$job
->lasterror;
}
my
@result
= ();
foreach
my
$job
(
$self
->driver->get_jobs ) {
my
$lasterror_msg
=
$job
->lasterror_msg;
chomp
$lasterror_msg
;
push
@result
,
sprintf
(
"%-$max[0]s %$max[1]d %$max[2]d %$max[3]d %$max[4]s %$max[5]s %s"
,
$job
->name,
$job
->min_processes,
$job
->max_processes,
$job
->count_processes,
DateTime->from_epoch(
epoch
=>
$job
->lastrun ),
DateTime->from_epoch(
epoch
=>
$job
->lasterror ),
$lasterror_msg
?
"ERROR: $lasterror_msg"
:
""
);
}
return
@result
;
}
sub
set_min_processes {
my
(
$self
,
$job_name
,
$min_processes
) =
@_
;
my
$job
=
$self
->get_job(
$job_name
);
if
( !
defined
(
$min_processes
) ||
$min_processes
!~ /^\d+$/ ||
$min_processes
< 0 ) {
die
"ERR invalid_value: min_processes must be >= 0\n"
;
}
if
(
$min_processes
>
$job
->max_processes ) {
die
"ERR invalid_value: min_processes must be smaller than max_processes\n"
;
}
$job
->min_processes(
$min_processes
);
return
"OK"
;
}
sub
set_max_processes {
my
(
$self
,
$job_name
,
$max_processes
) =
@_
;
my
$job
=
$self
->get_job(
$job_name
);
if
( !
defined
(
$max_processes
) ||
$max_processes
!~ /^\d+$/ ||
$max_processes
< 0 ) {
die
"ERR invalid_value: max_processes must be >= 0\n"
;
}
if
(
$max_processes
<
$job
->min_processes ) {
die
"ERR invalid_value: max_processes must be greater than min_processes\n"
;
}
$job
->max_processes(
$max_processes
);
return
"OK"
;
}
sub
set_processes {
my
(
$self
,
$job_name
,
$min_processes
,
$max_processes
) =
@_
;
my
$job
=
$self
->get_job(
$job_name
);
if
( !
defined
(
$min_processes
) ||
$min_processes
!~ /^\d+$/ ||
$min_processes
< 0 ) {
die
"ERR invalid_value: min_processes must be >= 0\n"
;
}
if
( !
defined
(
$max_processes
) ||
$max_processes
!~ /^\d+$/ ||
$max_processes
< 0 ) {
die
"ERR invalid_value: max_processes must be >= 0\n"
;
}
if
(
$max_processes
<
$min_processes
) {
die
"ERR invalid_value: max_processes must be greater than min_processes\n"
;
}
$job
->min_processes(
$min_processes
);
$job
->max_processes(
$max_processes
);
return
"OK"
;
}
sub
show {
my
(
$self
,
$job_name
) =
@_
;
my
$job
=
$self
->get_job(
$job_name
);
my
@result
= ();
my
$lasterror_msg
=
$job
->lasterror_msg;
chomp
$lasterror_msg
;
push
@result
,
sprintf
(
"%s %d %d %d %s %s %s"
,
$job
->name,
$job
->min_processes,
$job
->max_processes,
$job
->count_processes,
DateTime->from_epoch(
epoch
=>
$job
->lastrun ),
DateTime->from_epoch(
epoch
=>
$job
->lasterror ),
$lasterror_msg
?
"ERROR: $lasterror_msg"
:
""
);
push
@result
,
$job
->get_pids;
return
@result
;
}
sub
kill
{
my
(
$self
,
@pids
) =
@_
;
my
@valid_pids
= ();
foreach
my
$job
(
$self
->driver->get_jobs ) {
my
@job_pids
=
$job
->get_pids;
foreach
my
$pid
(
@pids
) {
if
(
grep
$_
eq
$pid
,
@job_pids
) {
push
@valid_pids
,
$pid
;
}
}
}
die
"ERR invalid_value: the given PID(s) do not belong to us\n"
unless
@valid_pids
;
CORE::
kill
15,
@valid_pids
;
return
"OK"
;
}
sub
killall {
my
(
$self
,
@job_names
) =
@_
;
die
"ERR invalid_value: no job_names given\n"
unless
scalar
(
@job_names
);
my
$kill
=
sub
{
my
(
$job
) =
@_
;
my
@pids
=
$job
->get_pids;
CORE::
kill
15,
@pids
;
Time::HiRes::usleep(50);
};
if
(
defined
$job_names
[0] &&
$job_names
[0] eq
'*'
&&
scalar
(
@job_names
) == 1 ) {
foreach
my
$job
(
$self
->driver->get_jobs ) {
$kill
->(
$job
);
}
}
else
{
foreach
my
$job_name
(
@job_names
) {
my
$job
=
$self
->get_job(
$job_name
);
$kill
->(
$job
);
}
}
return
"OK"
;
}
sub
shutdown
{
my
(
$self
) =
@_
;
$self
->driver->
shutdown
;
}
no
Moose::Role;
1;