]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Tasks.pm
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).",
23 permissions
=> { check
=> [ 'admin', 'audit' ] },
25 additionalProperties
=> 0,
27 node
=> get_standard_option
('pve-node'),
53 upid
=> { type
=> 'string' },
56 links
=> [ { rel
=> 'child', href
=> "{upid}" } ],
61 my $restenv = PMG
::RESTEnvironment-
>get();
65 my $filename = "/var/log/pve/tasks/index";
67 my $node = $param->{node
};
68 my $start = $param->{start
} || 0;
69 my $limit = $param->{limit
} || 50;
70 my $userfilter = $param->{userfilter
};
71 my $errors = $param->{errors
};
76 my $parse_line = sub {
77 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
81 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
82 return if $userfilter && $task->{user
} !~ m/\Q$userfilter\E/i;
83 return if $errors && $status && $status eq 'OK';
84 return if $count++ < $start;
85 return if $limit <= 0;
87 $task->{upid
} = $upid;
88 $task->{endtime
} = hex($endtime) if $endtime;
89 $task->{status
} = $status if $status;
96 if (my $bw = File
::ReadBackwards-
>new($filename)) {
97 while (defined ($line = $bw->readline)) {
102 if (my $bw = File
::ReadBackwards-
>new("$filename.1")) {
103 while (defined ($line = $bw->readline)) {
109 $restenv->set_result_attrib('total', $count);
114 __PACKAGE__-
>register_method({
115 name
=> 'upid_index',
118 description
=> '', # index helper
119 permissions
=> { check
=> [ 'admin', 'audit' ] },
121 additionalProperties
=> 0,
123 node
=> get_standard_option
('pve-node'),
124 upid
=> { type
=> 'string' },
133 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
144 __PACKAGE__-
>register_method({
148 description
=> 'Stop a task.',
151 permissions
=> { check
=> [ 'admin' ] },
153 additionalProperties
=> 0,
155 node
=> get_standard_option
('pve-node'),
156 upid
=> { type
=> 'string' },
159 returns
=> { type
=> 'null' },
163 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
164 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
165 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
167 my $restenv = PMG
::RESTEnvironment-
>get();
168 PMG
::RESTEnvironment-
>check_worker($param->{upid
}, 1);
173 __PACKAGE__-
>register_method({
174 name
=> 'read_task_log',
175 path
=> '{upid}/log',
178 description
=> "Read task log.",
180 permissions
=> { check
=> [ 'admin', 'audit' ] },
182 additionalProperties
=> 0,
184 node
=> get_standard_option
('pve-node'),
185 upid
=> { type
=> 'string' },
204 description
=> "Line number",
208 description
=> "Line text",
217 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
218 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
222 my $restenv = PMG
::RESTEnvironment-
>get();
224 my $fh = IO
::File-
>new($filename, "r");
225 raise_param_exc
({ upid
=> "no such task - unable to open file - $!" }) if !$fh;
227 my $start = $param->{start
} || 0;
228 my $limit = $param->{limit
} || 50;
231 while (defined ($line = <$fh>)) {
232 next if $count++ < $start;
235 push @$lines, { n
=> $count, t
=> $line};
241 # HACK: ExtJS store.guaranteeRange() does not like empty array
245 push @$lines, { n
=> $count, t
=> "no content"};
248 $restenv->set_result_attrib('total', $count);
254 my $exit_status_cache = {};
256 __PACKAGE__-
>register_method({
257 name
=> 'read_task_status',
258 path
=> '{upid}/status',
261 description
=> "Read task status.",
263 permissions
=> { check
=> [ 'admin', 'audit' ] },
265 additionalProperties
=> 0,
267 node
=> get_standard_option
('pve-node'),
268 upid
=> { type
=> 'string' },
278 type
=> 'string', enum
=> ['running', 'stopped'],
285 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
286 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
287 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
291 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
});
292 $task->{status
} = ($pstart && ($pstart == $task->{pstart
})) ?
293 'running' : 'stopped';
295 $task->{upid
} = $param->{upid
}; # include upid
297 if ($task->{status
} eq 'stopped') {
298 if (!defined($exit_status_cache->{$task->{upid
}})) {
299 $exit_status_cache->{$task->{upid
}} =
300 PVE
::Tools
::upid_read_status
($task->{upid
});
302 $task->{exitstatus
} = $exit_status_cache->{$task->{upid
}};