]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Tasks.pm
a369c2a7bc2c9d58ee20c3b9fb135dc2558b4156
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'),
49 description
=> 'Only list tasks of this type (e.g., aptupdate, saupdate).',
53 description
=> "Only list tasks since this UNIX epoch.",
58 description
=> "Only list tasks until this UNIX epoch.",
68 upid
=> { type
=> 'string' },
71 links
=> [ { rel
=> 'child', href
=> "{upid}" } ],
76 my $restenv = PMG
::RESTEnvironment-
>get();
80 my $filename = "/var/log/pve/tasks/index";
82 my $node = $param->{node
};
83 my $start = $param->{start
} || 0;
84 my $limit = $param->{limit
} || 50;
85 my $userfilter = $param->{userfilter
};
86 my $typefilter = $param->{typefilter
};
87 my $since = $param->{since
};
88 my $until = $param->{until};
89 my $errors = $param->{errors
};
94 my $parse_line = sub {
95 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
99 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
100 return if $userfilter && $task->{user
} !~ m/\Q$userfilter\E/i;
101 return if $errors && $status && $status eq 'OK';
102 return if defined($since) && $task->{starttime
} < $since;
103 return if defined($until) && $task->{starttime
} > $until;
104 return if $typefilter && $task->{type
} ne $typefilter;
106 return if $count++ < $start;
107 return if $limit <= 0;
109 $task->{upid
} = $upid;
110 $task->{endtime
} = hex($endtime) if $endtime;
111 $task->{status
} = $status if $status;
118 if (my $bw = File
::ReadBackwards-
>new($filename)) {
119 while (defined ($line = $bw->readline)) {
124 if (my $bw = File
::ReadBackwards-
>new("$filename.1")) {
125 while (defined ($line = $bw->readline)) {
131 $restenv->set_result_attrib('total', $count);
136 __PACKAGE__-
>register_method({
137 name
=> 'upid_index',
140 description
=> '', # index helper
141 permissions
=> { check
=> [ 'admin', 'audit' ] },
143 additionalProperties
=> 0,
145 node
=> get_standard_option
('pve-node'),
146 upid
=> { type
=> 'string' },
155 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
166 __PACKAGE__-
>register_method({
170 description
=> 'Stop a task.',
173 permissions
=> { check
=> [ 'admin' ] },
175 additionalProperties
=> 0,
177 node
=> get_standard_option
('pve-node'),
178 upid
=> { type
=> 'string' },
181 returns
=> { type
=> 'null' },
185 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
186 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
187 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
189 my $restenv = PMG
::RESTEnvironment-
>get();
190 PMG
::RESTEnvironment-
>check_worker($param->{upid
}, 1);
195 __PACKAGE__-
>register_method({
196 name
=> 'read_task_log',
197 path
=> '{upid}/log',
200 description
=> "Read task log.",
202 permissions
=> { check
=> [ 'admin', 'audit' ] },
204 additionalProperties
=> 0,
206 node
=> get_standard_option
('pve-node'),
207 upid
=> { type
=> 'string' },
226 description
=> "Line number",
230 description
=> "Line text",
239 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
240 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
244 my $restenv = PMG
::RESTEnvironment-
>get();
246 my $fh = IO
::File-
>new($filename, "r");
247 raise_param_exc
({ upid
=> "no such task - unable to open file - $!" }) if !$fh;
249 my $start = $param->{start
} || 0;
250 my $limit = $param->{limit
} || 50;
253 while (defined ($line = <$fh>)) {
254 next if $count++ < $start;
257 push @$lines, { n
=> $count, t
=> $line};
263 # HACK: ExtJS store.guaranteeRange() does not like empty array
267 push @$lines, { n
=> $count, t
=> "no content"};
270 $restenv->set_result_attrib('total', $count);
276 my $exit_status_cache = {};
278 __PACKAGE__-
>register_method({
279 name
=> 'read_task_status',
280 path
=> '{upid}/status',
283 description
=> "Read task status.",
285 permissions
=> { check
=> [ 'admin', 'audit' ] },
287 additionalProperties
=> 0,
289 node
=> get_standard_option
('pve-node'),
290 upid
=> { type
=> 'string' },
300 type
=> 'string', enum
=> ['running', 'stopped'],
307 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
308 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
309 raise_param_exc
({ upid
=> "no such task" }) if ! -f
$filename;
313 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
});
314 $task->{status
} = ($pstart && ($pstart == $task->{pstart
})) ?
315 'running' : 'stopped';
317 $task->{upid
} = $param->{upid
}; # include upid
319 if ($task->{status
} eq 'stopped') {
320 if (!defined($exit_status_cache->{$task->{upid
}})) {
321 $exit_status_cache->{$task->{upid
}} =
322 PVE
::Tools
::upid_read_status
($task->{upid
});
324 $task->{exitstatus
} = $exit_status_cache->{$task->{upid
}};