]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Tasks.pm
81ff8ca01c2eec4e507a27c806ce9cc4b4640b63
1 package PMG
::API2
::Tasks
;
7 use File
::ReadBackwards
;
12 use PMG
::RESTEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::RESTHandler);
17 __PACKAGE__-
>register_method({
21 description
=> "Read task list for one node (finished tasks).",
24 additionalProperties
=> 0,
26 node
=> get_standard_option
('pve-node'),
52 upid
=> { type
=> 'string' },
55 links
=> [ { rel
=> 'child', href
=> "{upid}" } ],
60 my $restenv = PMG
::RESTEnvironment
::get
();
64 my $filename = "/var/log/pve/tasks/index";
66 my $node = $param->{node
};
67 my $start = $param->{start
} || 0;
68 my $limit = $param->{limit
} || 50;
69 my $userfilter = $param->{userfilter
};
70 my $errors = $param->{errors
};
75 my $parse_line = sub {
76 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
80 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
81 return if $userfilter && $task->{user
} !~ m/\Q$userfilter\E/i;
82 return if $errors && $status && $status eq 'OK';
83 return if $count++ < $start;
84 return if $limit <= 0;
86 $task->{upid
} = $upid;
87 $task->{endtime
} = hex($endtime) if $endtime;
88 $task->{status
} = $status if $status;
95 if (my $bw = File
::ReadBackwards-
>new($filename)) {
96 while (defined ($line = $bw->readline)) {
101 if (my $bw = File
::ReadBackwards-
>new("$filename.1")) {
102 while (defined ($line = $bw->readline)) {
108 $restenv->set_result_attrib('total', $count);
113 __PACKAGE__-
>register_method({
114 name
=> 'upid_index',
117 description
=> '', # index helper
119 additionalProperties
=> 0,
121 node
=> get_standard_option
('pve-node'),
122 upid
=> { type
=> 'string' },
131 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
142 __PACKAGE__-
>register_method({
146 description
=> 'Stop a task.',
150 additionalProperties
=> 0,
152 node
=> get_standard_option
('pve-node'),
153 upid
=> { type
=> 'string' },
156 returns
=> { type
=> 'null' },
160 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
161 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
162 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
164 my $restenv = PMG
::RESTEnvironment
::get
();
165 PMG
::RESTEnvironment-
>check_worker($param->{upid
}, 1);
170 __PACKAGE__-
>register_method({
171 name
=> 'read_task_log',
172 path
=> '{upid}/log',
175 description
=> "Read task log.",
178 additionalProperties
=> 0,
180 node
=> get_standard_option
('pve-node'),
181 upid
=> { type
=> 'string' },
200 description
=> "Line number",
204 description
=> "Line text",
213 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
214 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
218 my $restenv = PMG
::RESTEnvironment
::get
();
220 my $fh = IO
::File-
>new($filename, "r");
221 raise_param_exc
({ upid
=> "no such task - unable to open file - $!" }) if !$fh;
223 my $start = $param->{start
} || 0;
224 my $limit = $param->{limit
} || 50;
227 while (defined ($line = <$fh>)) {
228 next if $count++ < $start;
231 push @$lines, { n
=> $count, t
=> $line};
237 # HACK: ExtJS store.guaranteeRange() does not like empty array
241 push @$lines, { n
=> $count, t
=> "no content"};
244 $restenv->set_result_attrib('total', $count);
250 my $exit_status_cache = {};
252 __PACKAGE__-
>register_method({
253 name
=> 'read_task_status',
254 path
=> '{upid}/status',
257 description
=> "Read task status.",
260 additionalProperties
=> 0,
262 node
=> get_standard_option
('pve-node'),
263 upid
=> { type
=> 'string' },
273 type
=> 'string', enum
=> ['running', 'stopped'],
280 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
281 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
282 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
286 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
});
287 $task->{status
} = ($pstart && ($pstart == $task->{pstart
})) ?
288 'running' : 'stopped';
290 $task->{upid
} = $param->{upid
}; # include upid
292 if ($task->{status
} eq 'stopped') {
293 if (!defined($exit_status_cache->{$task->{upid
}})) {
294 $exit_status_cache->{$task->{upid
}} =
295 PVE
::Tools
::upid_read_status
($task->{upid
});
297 $task->{exitstatus
} = $exit_status_cache->{$task->{upid
}};