-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathAsyncTask.vala
More file actions
526 lines (422 loc) · 12.1 KB
/
AsyncTask.vala
File metadata and controls
526 lines (422 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* AsyncTask.vala
*
* Copyright 2012-2018 Tony George <teejeetech@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.System;
using TeeJee.Misc;
public abstract class AsyncTask : GLib.Object{
private string err_line = "";
private string out_line = "";
private DataOutputStream dos_in = null;
private DataInputStream dis_out = null;
private DataInputStream dis_err = null;
protected DataOutputStream dos_log = null;
private bool stdout_is_open = false;
private bool stderr_is_open = false;
protected Pid child_pid = 0;
private int input_fd = -1;
private int output_fd = -1;
private int error_fd = -1;
private bool finish_called = false;
protected string script_file = "";
protected string working_dir = "";
// public
public AppStatus status = AppStatus.NOT_STARTED;
private string _status_line = "";
public GLib.Mutex status_line_mutex;
public int exit_code = 0;
public GLib.Timer timer;
private double timerOffset = 0.0; // milliseconds to be added to the current timer - this is to compensate for pauses (timer restarts)
public double progress = 0.0;
public double percent = 0.0;
public int64 prg_count = 0;
public int64 prg_count_total = 0;
public bool io_nice = true; // renice child processes to IDlE PRIO
// signals
public signal void stdout_line_read(string line);
public signal void stderr_line_read(string line);
public signal void task_complete();
[CCode(notify = false)]
public string status_line
{
owned get {
return _get_status_line();
}
set
{
status_line_mutex.lock();
_status_line = value;
status_line_mutex.unlock();
}
}
private string _get_status_line() {
string ret = "";
if (status_line_mutex.trylock()) {
ret = _status_line;
status_line_mutex.unlock();
}
return ret;
}
protected AsyncTask(){
working_dir = TEMP_DIR + "/" + timestamp_for_path();
script_file = path_combine(working_dir, "script.sh");
status_line_mutex = GLib.Mutex();
dir_create(working_dir);
}
public virtual void prepare() {
string script_text = build_script();
log_debug(script_text);
save_bash_script_temp(script_text, script_file);
log_debug("AsyncTask:prepare(): saved: %s".printf(script_file));
}
protected abstract string build_script();
protected virtual bool begin() {
status = AppStatus.RUNNING;
bool has_started = true;
finish_called = false;
prg_count = 0;
string[] spawn_args = new string[1];
spawn_args[0] = script_file;
string[] spawn_env = Environ.get();
try {
// start timer
timer = new GLib.Timer();
timer.start();
GLib.SpawnChildSetupFunc? childsetup = null;
if(this.io_nice) {
// change io prio of process, right before it execs
childsetup = () => TeeJee.ProcessHelper.ioprio_set(0, IoPrio.prioValue(IoPrio.PrioClass.IDLE, 0));
}
// execute script file
Process.spawn_async_with_pipes(
working_dir, // working dir
spawn_args, // argv
spawn_env, // environment
SpawnFlags.SEARCH_PATH,
childsetup, // child_setup
out child_pid,
out input_fd,
out output_fd,
out error_fd);
log_debug("AsyncTask: child_pid: %d".printf(child_pid));
// create stream readers
UnixOutputStream uos_in = new UnixOutputStream(input_fd, true);
UnixInputStream uis_out = new UnixInputStream(output_fd, true);
UnixInputStream uis_err = new UnixInputStream(error_fd, true);
dos_in = new DataOutputStream(uos_in);
dis_out = new DataInputStream(uis_out);
dis_err = new DataInputStream(uis_err);
dis_out.newline_type = DataStreamNewlineType.ANY;
dis_err.newline_type = DataStreamNewlineType.ANY;
try {
//start thread for reading output stream
new Thread<void>.try ("async-task-stdout-reader", read_stdout);
} catch (GLib.Error e) {
log_error ("AsyncTask.begin():create_thread:read_stdout()");
log_error (e.message);
}
try {
//start thread for reading error stream
new Thread<void>.try ("async-task-stderr-reader", read_stderr);
} catch (GLib.Error e) {
log_error ("AsyncTask.begin():create_thread:read_stderr()");
log_error (e.message);
}
}
catch (Error e) {
log_error ("AsyncTask.begin()");
log_error(e.message);
has_started = false;
//status = AppStatus.FINISHED;
}
return has_started;
}
private void read_stdout() {
try {
stdout_is_open = true;
out_line = dis_out.read_line (null);
while (out_line != null) {
//log_msg("O: " + out_line);
if (out_line.length > 0) {
parse_stdout_line(out_line);
stdout_line_read(out_line); //signal
}
out_line = dis_out.read_line (null); //read next
}
stdout_is_open = false;
// dispose stdout
try {
if (dis_out != null) {
dis_out.close();
}
}
catch (GLib.Error ignored) {}
dis_out = null;
// check if complete
if (!stdout_is_open && !stderr_is_open){
finish();
}
}
catch (Error e) {
log_error ("AsyncTask.read_stdout()");
log_error (e.message);
}
}
private void read_stderr() {
try {
stderr_is_open = true;
err_line = dis_err.read_line (null);
while (err_line != null) {
if (err_line.length > 0){
parse_stderr_line(err_line);
stderr_line_read(err_line); //signal
}
err_line = dis_err.read_line (null); //read next
}
stderr_is_open = false;
// dispose stderr
try {
if (dis_err != null) {
dis_err.close();
}
}
catch (GLib.Error ignored) {}
dis_err = null;
// check if complete
if (!stdout_is_open && !stderr_is_open){
finish();
}
}
catch (Error e) {
log_error ("AsyncTask.read_stderr()");
log_error (e.message);
}
}
public void write_stdin(string line){
try{
if (status == AppStatus.RUNNING){
dos_in.put_string(line + "\n");
}
else{
log_error ("AsyncTask.write_stdin(): NOT RUNNING");
}
}
catch(Error e){
log_error ("AsyncTask.write_stdin(): %s".printf(line));
log_error (e.message);
}
}
public virtual void execute() {
log_debug("AsyncTask:execute()");
prepare();
begin();
}
protected abstract void parse_stdout_line(string out_line);
protected abstract void parse_stderr_line(string err_line);
private void finish(){
// finish() gets called by 2 threads but should be executed only once
if (finish_called) { return; }
finish_called = true;
log_debug("AsyncTask: finish(): enter");
// dispose stdin
try{
if (dos_in != null) {
dos_in.close();
}
}
catch(GLib.IOError e) {
// ignore
}
dos_in = null;
// dispose child process
Process.close_pid(child_pid); //required on Windows, doesn't do anything on Unix
read_exit_code();
status_line = "";
err_line = "";
out_line = "";
timer.stop();
finish_task();
if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) {
status = AppStatus.FINISHED;
}
//dir_delete(working_dir);
task_complete(); //signal
}
// can be overloaded by subclasses, that wish to do special stuff during finish
protected virtual void finish_task() {}
protected int read_exit_code(){
exit_code = -1;
var path = file_parent(script_file) + "/status";
if (file_exists(path)){
var txt = file_read(path);
exit_code = int.parse(txt);
}
log_debug("exit_code: %d".printf(exit_code));
return exit_code;
}
public bool is_running(){
return (status == AppStatus.RUNNING);
}
// public actions --------------
public void stop(AppStatus status_to_update = AppStatus.CANCELLED) {
status = status_to_update;
if(0 != child_pid) {
process_quit(child_pid);
child_pid = 0;
log_debug("process_quit: %d ".printf(child_pid));
}
}
public void pause(AppStatus status_to_update = AppStatus.PAUSED) {
status = status_to_update;
if(0 != child_pid) {
TeeJee.ProcessHelper.process_send_signal(this.child_pid, Posix.Signal.STOP, true);
// "pause" timer
this.timerOffset += TeeJee.System.timer_elapsed(this.timer, true);
log_debug("process_paused: %d ".printf(this.child_pid));
}
}
// unpause (continue) the task
public void resume(AppStatus status_to_update = AppStatus.RUNNING) {
status = status_to_update;
if(0 != child_pid) {
TeeJee.ProcessHelper.process_send_signal(this.child_pid, Posix.Signal.CONT, true);
// restart timer
this.timer = new GLib.Timer();
this.timer.start();
log_debug("process_resumed: %d ".printf(this.child_pid));
}
}
private double elapsed {
get {
double elapsed = timerOffset;
if(this.status != AppStatus.PAUSED) {
elapsed += TeeJee.System.timer_elapsed(timer);
}
return elapsed;
}
}
public string stat_time_elapsed{
owned get{
return TeeJee.Misc.format_duration(this.elapsed);
}
}
public string stat_time_remaining{
owned get{
if (this.progress > 0){
double remaining = ((this.elapsed / this.progress) * (1.0 - this.progress));
if (remaining < 0){
remaining = 0;
}
return TeeJee.Misc.format_duration(remaining);
}
else{
return "???";
}
}
}
public void print_app_status(){
switch(status){
case AppStatus.NOT_STARTED:
log_debug("status=%s".printf("NOT_STARTED"));
break;
case AppStatus.RUNNING:
log_debug("status=%s".printf("RUNNING"));
break;
case AppStatus.PAUSED:
log_debug("status=%s".printf("PAUSED"));
break;
case AppStatus.FINISHED:
log_debug("status=%s".printf("FINISHED"));
break;
case AppStatus.CANCELLED:
log_debug("status=%s".printf("CANCELLED"));
break;
case AppStatus.PASSWORD_REQUIRED:
log_debug("status=%s".printf("PASSWORD_REQUIRED"));
break;
}
}
}
public enum AppStatus {
NOT_STARTED,
RUNNING,
PAUSED,
FINISHED,
CANCELLED,
PASSWORD_REQUIRED
}
/* Sample Subclass:
public class RsyncTask : AsyncTask{
public bool delete_extra = true;
public string rsync_log_file = "";
public string exclude_from_file = "";
public string source_path = "";
public string dest_path = "";
public bool verbose = true;
public RsyncTask(string _script_file, string _working_dir, string _log_file){
working_dir = _working_dir;
script_file = _script_file;
log_file = _log_file;
}
protected override string build_script() {
var cmd = "rsync -ai";
if (verbose){
cmd += " --verbose";
}
else{
cmd += " --quiet";
}
if (delete_extra){
cmd += " --delete";
}
cmd += " --numeric-ids --stats --relative --delete-excluded";
if (rsync_log_file.length > 0){
cmd += " --log-file='%s'".printf(escape_single_quote(rsync_log_file));
}
if (exclude_from_file.length > 0){
cmd += " --exclude-from='%s'".printf(escape_single_quote(exclude_from_file));
}
source_path = remove_trailing_slash(source_path);
dest_path = remove_trailing_slash(dest_path);
cmd += " '%s/'".printf(escape_single_quote(source_path));
cmd += " '%s/'".printf(escape_single_quote(dest_path));
//cmd += " /. \"%s\"".printf(sync_path + "/localhost/");
return cmd;
}
// execution ----------------------------
public override void parse_stdout_line(string out_line){
update_progress_parse_console_output(out_line);
}
public override void parse_stderr_line(string err_line){
update_progress_parse_console_output(err_line);
}
public bool update_progress_parse_console_output (string line) {
if ((line == null) || (line.length == 0)) {
return true;
}
return true;
}
}
*/