Skip to content
API Documentation
BastionGuard™
AsyncTask Class Referenceabstract
Inheritance diagram for AsyncTask:
DeleteFileTask RsyncSpaceCheckTask RsyncTask

Public Member Functions

signal void stdout_line_read (string line)
signal void stderr_line_read (string line)
signal void task_complete ()
virtual void prepare ()
void write_stdin (string line)
virtual void execute ()
bool is_running ()
void stop (AppStatus status_to_update=AppStatus.CANCELLED)
void pause (AppStatus status_to_update=AppStatus.PAUSED)
void resume (AppStatus status_to_update=AppStatus.RUNNING)
void print_app_status ()

Public Attributes

AppStatus status = AppStatus.NOT_STARTED
GLib.Mutex status_line_mutex
int exit_code = 0
GLib.Timer timer
double progress = 0.0
double percent = 0.0
int64 prg_count = 0
int64 prg_count_total = 0

Protected Member Functions

 AsyncTask ()
string build_script ()
virtual bool begin ()
void parse_stdout_line (string out_line)
void parse_stderr_line (string err_line)
virtual void finish_task ()
int read_exit_code ()

Protected Attributes

DataOutputStream dos_log = null
Pid child_pid = 0
string script_file = ""
string working_dir = ""

Properties

string status_line [get, set]
double elapsed [get]
string stat_time_elapsed [get]
string??? stat_time_remaining [get]

Private Member Functions

string _get_status_line ()
void read_stdout ()
void read_stderr ()
void finish ()

Private Attributes

string err_line = ""
string out_line = ""
DataOutputStream dos_in = null
DataInputStream dis_out = null
DataInputStream dis_err = null
bool stdout_is_open = false
bool stderr_is_open = false
int input_fd = -1
int output_fd = -1
int error_fd = -1
bool finish_called = false
string _status_line = ""
double timerOffset = 0.0

Detailed Description

Definition at line 31 of file AsyncTask.vala.

Constructor & Destructor Documentation

◆ AsyncTask()

AsyncTask.AsyncTask ( )
inlineprotected

Definition at line 97 of file AsyncTask.vala.

97 {
99 script_file = path_combine(working_dir, "script.sh");
100
101 status_line_mutex = GLib.Mutex();
102
104 }
GLib.Mutex status_line_mutex
string script_file
string working_dir
string path_combine(string path1, string path2)
bool dir_create(string dir_path, bool show_message=false)
string timestamp_for_path()

References TeeJee.FileSystem.dir_create(), TeeJee.FileSystem.path_combine(), script_file, status_line_mutex, TeeJee.ProcessHelper.TEMP_DIR, TeeJee.Misc.timestamp_for_path(), and working_dir.

Member Function Documentation

◆ _get_status_line()

string AsyncTask._get_status_line ( )
inlineprivate

Definition at line 86 of file AsyncTask.vala.

86 {
87 string ret = "";
88
89 if (status_line_mutex.trylock()) {
90 ret = _status_line;
91 status_line_mutex.unlock();
92 }
93
94 return ret;
95 }
string _status_line

References _status_line, and status_line_mutex.

◆ begin()

virtual bool AsyncTask.begin ( )
inlineprotectedvirtual

Definition at line 116 of file AsyncTask.vala.

116 {
117 status = AppStatus.RUNNING;
118
119 bool has_started = true;
120 finish_called = false;
121
122 prg_count = 0;
123
124 string[] spawn_args = new string[1];
125 spawn_args[0] = script_file;
126
127 string[] spawn_env = Environ.get();
128
129 try {
130 // start timer
131 timer = new GLib.Timer();
132 timer.start();
133
134 // execute script file
135 Process.spawn_async_with_pipes(
136 working_dir, // working dir
137 spawn_args, // argv
138 spawn_env, // environment
139 SpawnFlags.SEARCH_PATH,
140 null, // child_setup
141 out child_pid,
142 out input_fd,
143 out output_fd,
144 out error_fd);
145
146 log_debug("AsyncTask: child_pid: %d".printf(child_pid));
147
148 // create stream readers
149 UnixOutputStream uos_in = new UnixOutputStream(input_fd, true);
150 UnixInputStream uis_out = new UnixInputStream(output_fd, true);
151 UnixInputStream uis_err = new UnixInputStream(error_fd, true);
152 dos_in = new DataOutputStream(uos_in);
153 dis_out = new DataInputStream(uis_out);
154 dis_err = new DataInputStream(uis_err);
155 dis_out.newline_type = DataStreamNewlineType.ANY;
156 dis_err.newline_type = DataStreamNewlineType.ANY;
157
158 try {
159 //start thread for reading output stream
160 new Thread<void>.try ("async-task-stdout-reader", read_stdout);
161 } catch (GLib.Error e) {
162 log_error ("AsyncTask.begin():create_thread:read_stdout()");
163 log_error (e.message);
164 }
165
166 try {
167 //start thread for reading error stream
168 new Thread<void>.try ("async-task-stderr-reader", read_stderr);
169 } catch (GLib.Error e) {
170 log_error ("AsyncTask.begin():create_thread:read_stderr()");
171 log_error (e.message);
172 }
173 }
174 catch (Error e) {
175 log_error ("AsyncTask.begin()");
176 log_error(e.message);
177 has_started = false;
178 //status = AppStatus.FINISHED;
179 }
180
181 return has_started;
182 }
AppStatus
int output_fd
void read_stderr()
void read_stdout()
int64 prg_count
GLib.Timer timer
Pid child_pid
DataOutputStream dos_in
AppStatus status
DataInputStream dis_out
bool finish_called
DataInputStream dis_err
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)

References child_pid, dis_err, dis_out, dos_in, Error, error_fd, finish_called, input_fd, TeeJee.Logging.log_debug(), TeeJee.Logging.log_error(), output_fd, prg_count, read_stderr(), read_stdout(), script_file, status, timer, and working_dir.

Referenced by execute().

◆ build_script()

string AsyncTask.build_script ( )
abstractprotected

Referenced by prepare().

◆ execute()

virtual void AsyncTask.execute ( )
inlinevirtual

Definition at line 270 of file AsyncTask.vala.

270 {
271 log_debug("AsyncTask:execute()");
272 prepare();
273 begin();
274 }
virtual void prepare()
virtual bool begin()

References begin(), TeeJee.Logging.log_debug(), and prepare().

Referenced by Main.create_snapshot_with_rsync(), Main.estimate_system_size(), Main.get_space_needed_for_rsync_snapshot(), Snapshot.remove_rsync(), and Main.restore_other_gui().

◆ finish()

void AsyncTask.finish ( )
inlineprivate

Definition at line 280 of file AsyncTask.vala.

280 {
281
282 // finish() gets called by 2 threads but should be executed only once
283 if (finish_called) { return; }
284 finish_called = true;
285
286 log_debug("AsyncTask: finish(): enter");
287
288 // dispose stdin
289 try{
290 if (dos_in != null) {
291 dos_in.close();
292 }
293 }
294 catch(GLib.IOError e) {
295 // ignore
296 }
297 dos_in = null;
298
299 // dispose child process
300 Process.close_pid(child_pid); //required on Windows, doesn't do anything on Unix
301
303
304 status_line = "";
305 err_line = "";
306 out_line = "";
307
308 timer.stop();
309
310 finish_task();
311
312 if ((status != AppStatus.CANCELLED) && (status != AppStatus.PASSWORD_REQUIRED)) {
313 status = AppStatus.FINISHED;
314 }
315
316 //dir_delete(working_dir);
317
318 task_complete(); //signal
319 }
string err_line
int read_exit_code()
virtual void finish_task()
string status_line
signal void task_complete()
string out_line

References child_pid, dos_in, err_line, finish_called, finish_task(), TeeJee.Logging.log_debug(), out_line, read_exit_code(), status, status_line, task_complete(), and timer.

Referenced by read_stderr(), and read_stdout().

◆ finish_task()

virtual void AsyncTask.finish_task ( )
inlineprotectedvirtual

Reimplemented in RsyncTask.

Definition at line 322 of file AsyncTask.vala.

322{}

Referenced by finish().

◆ is_running()

bool AsyncTask.is_running ( )
inline

Definition at line 336 of file AsyncTask.vala.

336 {
337 return (status == AppStatus.RUNNING);
338 }

References status.

◆ parse_stderr_line()

void AsyncTask.parse_stderr_line ( string err_line)
abstractprotected

References err_line.

Referenced by read_stderr().

◆ parse_stdout_line()

void AsyncTask.parse_stdout_line ( string out_line)
abstractprotected

References out_line.

Referenced by read_stdout().

◆ pause()

void AsyncTask.pause ( AppStatus status_to_update = AppStatus::PAUSED)
inline

Definition at line 353 of file AsyncTask.vala.

353 {
354 status = status_to_update;
355
356 if(0 != child_pid) {
357 TeeJee.ProcessHelper.process_send_signal(this.child_pid, Posix.Signal.STOP, true);
358
359 // "pause" timer
360 this.timerOffset += TeeJee.System.timer_elapsed(this.timer, true);
361
362 log_debug("process_paused: %d ".printf(this.child_pid));
363 }
364 }

References child_pid, TeeJee.Logging.log_debug(), status, and timer.

Referenced by BackupWindow.create_actions().

◆ prepare()

virtual void AsyncTask.prepare ( )
inlinevirtual

Reimplemented in DeleteFileTask, RsyncSpaceCheckTask, and RsyncTask.

Definition at line 106 of file AsyncTask.vala.

106 {
107 string script_text = build_script();
108 log_debug(script_text);
110
111 log_debug("AsyncTask:prepare(): saved: %s".printf(script_file));
112 }
string build_script()
string? save_bash_script_temp(string commands, string? script_path=null, bool force_locale=true, bool supress_errors=false)

References build_script(), TeeJee.Logging.log_debug(), TeeJee.ProcessHelper.save_bash_script_temp(), and script_file.

Referenced by execute().

◆ print_app_status()

void AsyncTask.print_app_status ( )
inline

Definition at line 412 of file AsyncTask.vala.

412 {
413
414 switch(status){
415 case AppStatus.NOT_STARTED:
416 log_debug("status=%s".printf("NOT_STARTED"));
417 break;
418 case AppStatus.RUNNING:
419 log_debug("status=%s".printf("RUNNING"));
420 break;
421 case AppStatus.PAUSED:
422 log_debug("status=%s".printf("PAUSED"));
423 break;
424 case AppStatus.FINISHED:
425 log_debug("status=%s".printf("FINISHED"));
426 break;
427 case AppStatus.CANCELLED:
428 log_debug("status=%s".printf("CANCELLED"));
429 break;
430 case AppStatus.PASSWORD_REQUIRED:
431 log_debug("status=%s".printf("PASSWORD_REQUIRED"));
432 break;
433 }
434 }

References TeeJee.Logging.log_debug(), and status.

◆ read_exit_code()

int AsyncTask.read_exit_code ( )
inlineprotected

Definition at line 324 of file AsyncTask.vala.

324 {
325
326 exit_code = -1;
327 var path = file_parent(script_file) + "/status";
328 if (file_exists(path)){
329 var txt = file_read(path);
330 exit_code = int.parse(txt);
331 }
332 log_debug("exit_code: %d".printf(exit_code));
333 return exit_code;
334 }
int exit_code
bool file_exists(string file_path)
string? file_read(string file_path)
string file_parent(string file_path)

References exit_code, TeeJee.FileSystem.file_exists(), TeeJee.FileSystem.file_parent(), TeeJee.FileSystem.file_read(), TeeJee.Logging.log_debug(), and script_file.

Referenced by finish().

◆ read_stderr()

void AsyncTask.read_stderr ( )
inlineprivate

Definition at line 220 of file AsyncTask.vala.

220 {
221 try {
222 stderr_is_open = true;
223
224 err_line = dis_err.read_line (null);
225 while (err_line != null) {
226 if (err_line.length > 0){
228 stderr_line_read(err_line); //signal
229 }
230 err_line = dis_err.read_line (null); //read next
231 }
232
233 stderr_is_open = false;
234
235 // dispose stderr
236 try {
237 if (dis_err != null) {
238 dis_err.close();
239 }
240 }
241 catch (GLib.Error ignored) {}
242 dis_err = null;
243
244 // check if complete
246 finish();
247 }
248 }
249 catch (Error e) {
250 log_error ("AsyncTask.read_stderr()");
251 log_error (e.message);
252 }
253 }
void parse_stderr_line(string err_line)
bool stdout_is_open
bool stderr_is_open
signal void stderr_line_read(string line)
void finish()

References dis_err, err_line, Error, finish(), TeeJee.Logging.log_error(), parse_stderr_line(), stderr_is_open, stderr_line_read(), and stdout_is_open.

Referenced by begin().

◆ read_stdout()

void AsyncTask.read_stdout ( )
inlineprivate

Definition at line 184 of file AsyncTask.vala.

184 {
185 try {
186 stdout_is_open = true;
187
188 out_line = dis_out.read_line (null);
189 while (out_line != null) {
190 //log_msg("O: " + out_line);
191 if (out_line.length > 0) {
193 stdout_line_read(out_line); //signal
194 }
195 out_line = dis_out.read_line (null); //read next
196 }
197
198 stdout_is_open = false;
199
200 // dispose stdout
201 try {
202 if (dis_out != null) {
203 dis_out.close();
204 }
205 }
206 catch (GLib.Error ignored) {}
207 dis_out = null;
208
209 // check if complete
211 finish();
212 }
213 }
214 catch (Error e) {
215 log_error ("AsyncTask.read_stdout()");
216 log_error (e.message);
217 }
218 }
signal void stdout_line_read(string line)
void parse_stdout_line(string out_line)

References dis_out, Error, finish(), TeeJee.Logging.log_error(), out_line, parse_stdout_line(), stderr_is_open, stdout_is_open, and stdout_line_read().

Referenced by begin().

◆ resume()

void AsyncTask.resume ( AppStatus status_to_update = AppStatus::RUNNING)
inline

Definition at line 367 of file AsyncTask.vala.

367 {
368 status = status_to_update;
369
370 if(0 != child_pid) {
371 TeeJee.ProcessHelper.process_send_signal(this.child_pid, Posix.Signal.CONT, true);
372
373 // restart timer
374 this.timer = new GLib.Timer();
375 this.timer.start();
376
377 log_debug("process_resumed: %d ".printf(this.child_pid));
378 }
379 }

References child_pid, TeeJee.Logging.log_debug(), status, and timer.

Referenced by BackupWindow.create_actions().

◆ stderr_line_read()

signal void AsyncTask.stderr_line_read ( string line)

Referenced by read_stderr().

◆ stdout_line_read()

signal void AsyncTask.stdout_line_read ( string line)

Referenced by read_stdout().

◆ stop()

void AsyncTask.stop ( AppStatus status_to_update = AppStatus::CANCELLED)
inline

Definition at line 342 of file AsyncTask.vala.

342 {
343 status = status_to_update;
344
345 if(0 != child_pid) {
347 child_pid = 0;
348
349 log_debug("process_quit: %d ".printf(child_pid));
350 }
351 }
void process_quit(Pid process_pid, bool killChildren=true)

References child_pid, TeeJee.Logging.log_debug(), TeeJee.ProcessHelper.process_quit(), and status.

Referenced by BackupWindow.create_actions(), DeleteWindow.create_actions(), RestoreWindow.create_actions(), SetupWizardWindow.create_actions(), and MainWindow.on_close_request().

◆ task_complete()

signal void AsyncTask.task_complete ( )

Referenced by finish().

◆ write_stdin()

void AsyncTask.write_stdin ( string line)
inline

Definition at line 255 of file AsyncTask.vala.

255 {
256 try{
257 if (status == AppStatus.RUNNING){
258 dos_in.put_string(line + "\n");
259 }
260 else{
261 log_error ("AsyncTask.write_stdin(): NOT RUNNING");
262 }
263 }
264 catch(Error e){
265 log_error ("AsyncTask.write_stdin(): %s".printf(line));
266 log_error (e.message);
267 }
268 }

References dos_in, Error, TeeJee.Logging.log_error(), and status.

Member Data Documentation

◆ _status_line

string AsyncTask._status_line = ""
private

Definition at line 55 of file AsyncTask.vala.

Referenced by _get_status_line().

◆ child_pid

Pid AsyncTask.child_pid = 0
protected

Definition at line 43 of file AsyncTask.vala.

Referenced by begin(), finish(), pause(), resume(), and stop().

◆ dis_err

DataInputStream AsyncTask.dis_err = null
private

Definition at line 37 of file AsyncTask.vala.

Referenced by begin(), and read_stderr().

◆ dis_out

DataInputStream AsyncTask.dis_out = null
private

Definition at line 36 of file AsyncTask.vala.

Referenced by begin(), and read_stdout().

◆ dos_in

DataOutputStream AsyncTask.dos_in = null
private

Definition at line 35 of file AsyncTask.vala.

Referenced by begin(), finish(), and write_stdin().

◆ dos_log

DataOutputStream AsyncTask.dos_log = null
protected

Definition at line 38 of file AsyncTask.vala.

◆ err_line

◆ error_fd

int AsyncTask.error_fd = -1
private

Definition at line 46 of file AsyncTask.vala.

Referenced by begin().

◆ exit_code

int AsyncTask.exit_code = 0

Definition at line 58 of file AsyncTask.vala.

Referenced by read_exit_code(), and RestoreBox.restore().

◆ finish_called

bool AsyncTask.finish_called = false
private

Definition at line 47 of file AsyncTask.vala.

Referenced by begin(), and finish().

◆ input_fd

int AsyncTask.input_fd = -1
private

Definition at line 44 of file AsyncTask.vala.

Referenced by begin().

◆ out_line

◆ output_fd

int AsyncTask.output_fd = -1
private

Definition at line 45 of file AsyncTask.vala.

Referenced by begin().

◆ percent

double AsyncTask.percent = 0.0

Definition at line 62 of file AsyncTask.vala.

◆ prg_count

◆ prg_count_total

◆ progress

◆ script_file

string AsyncTask.script_file = ""
protected

Definition at line 49 of file AsyncTask.vala.

Referenced by AsyncTask(), begin(), prepare(), and read_exit_code().

◆ status

◆ status_line_mutex

GLib.Mutex AsyncTask.status_line_mutex

Definition at line 56 of file AsyncTask.vala.

Referenced by _get_status_line(), and AsyncTask().

◆ stderr_is_open

bool AsyncTask.stderr_is_open = false
private

Definition at line 41 of file AsyncTask.vala.

Referenced by read_stderr(), and read_stdout().

◆ stdout_is_open

bool AsyncTask.stdout_is_open = false
private

Definition at line 40 of file AsyncTask.vala.

Referenced by read_stderr(), and read_stdout().

◆ timer

GLib.Timer AsyncTask.timer

Definition at line 59 of file AsyncTask.vala.

Referenced by begin(), finish(), pause(), and resume().

◆ timerOffset

double AsyncTask.timerOffset = 0.0
private

Definition at line 60 of file AsyncTask.vala.

◆ working_dir

string AsyncTask.working_dir = ""
protected

Definition at line 50 of file AsyncTask.vala.

Referenced by AsyncTask(), and begin().

Property Documentation

◆ elapsed

double AsyncTask.elapsed
getprivate

Definition at line 381 of file AsyncTask.vala.

381 {
382 get {
383 double elapsed = timerOffset;
384 if(this.status != AppStatus.PAUSED) {
385 elapsed += TeeJee.System.timer_elapsed(timer);
386 }
387 return elapsed;
388 }
389 }
double elapsed
double timerOffset

◆ stat_time_elapsed

string AsyncTask.stat_time_elapsed
get

Definition at line 391 of file AsyncTask.vala.

391 {
392 owned get{
393 return TeeJee.Misc.format_duration(this.elapsed);
394 }
395 }

Referenced by Main.delete_thread().

◆ stat_time_remaining

string??? AsyncTask.stat_time_remaining
get

Definition at line 397 of file AsyncTask.vala.

397 {
398 owned get{
399 if (this.progress > 0){
400 double remaining = ((this.elapsed / this.progress) * (1.0 - this.progress));
401 if (remaining < 0){
402 remaining = 0;
403 }
404 return TeeJee.Misc.format_duration(remaining);
405 }
406 else{
407 return "???";
408 }
409 }
410 }
double progress

Referenced by Main.create_snapshot_with_rsync(), Snapshot.remove_rsync(), and BackupBox.take_snapshot().

◆ status_line

string AsyncTask.status_line
getset

The documentation for this class was generated from the following file:
  • BastionGuard/thirdparty/bastionguard-backup/src/Utility/AsyncTask.vala