~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/TimeoutCounter.vala

  • Committer: Tony George
  • Date: 2016-10-16 13:42:12 UTC
  • Revision ID: tony.george.kol@gmail.com-20161016134212-ibjsptmgbha7a2mw
Fixed scheduled backups; Initialize display for the scheduled cron task;

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * TimeoutCounter.vala
 
4
 *
 
5
 * Copyright 2016 Tony George <teejeetech@gmail.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
 * MA 02110-1301, USA.
 
21
 *
 
22
 *
 
23
 */
 
24
 
 
25
 
 
26
using TeeJee.Logging;
 
27
using TeeJee.FileSystem;
 
28
using TeeJee.Misc;
 
29
 
 
30
public class TimeoutCounter : GLib.Object {
 
31
 
 
32
        public bool active = false;
 
33
        public string process_to_kill = "";
 
34
        public int seconds_to_wait = 30;
 
35
        public bool exit_app = false;
 
36
        
 
37
        public void kill_process_on_timeout(
 
38
                string process_to_kill, int seconds_to_wait = 20, bool exit_app = false){
 
39
 
 
40
                this.process_to_kill = process_to_kill;
 
41
                this.seconds_to_wait = seconds_to_wait;
 
42
                this.exit_app = exit_app;
 
43
                        
 
44
                try {
 
45
                        active = true;
 
46
                        Thread.create<void> (start_counter_thread, true);
 
47
                }
 
48
                catch (Error e) {
 
49
                        log_error (e.message);
 
50
                }
 
51
        }
 
52
 
 
53
        public void exit_on_timeout(int seconds_to_wait = 20){
 
54
                this.process_to_kill = "";
 
55
                this.seconds_to_wait = seconds_to_wait;
 
56
                this.exit_app = true;
 
57
                        
 
58
                try {
 
59
                        active = true;
 
60
                        Thread.create<void> (start_counter_thread, true);
 
61
                }
 
62
                catch (Error e) {
 
63
                        log_error (e.message);
 
64
                }
 
65
        }
 
66
 
 
67
        public void stop(){
 
68
                active = false;
 
69
        }
 
70
        
 
71
        public void start_counter_thread(){
 
72
                int secs = 0;
 
73
                
 
74
                while (active && (secs < seconds_to_wait)){
 
75
                        Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 1000);
 
76
                        secs += 1;
 
77
                }
 
78
 
 
79
                if (active){
 
80
                        active = false;
 
81
                        stdout.printf("\n");
 
82
 
 
83
                        if (process_to_kill.length > 0){
 
84
                                Posix.system("killall " + process_to_kill);
 
85
                                stderr.printf("\n[timeout] Killed process" + ": %s\n".printf(process_to_kill));
 
86
                        }
 
87
 
 
88
                        if (exit_app){
 
89
                                stderr.printf("\n[timeout] Exit application\n");
 
90
                                exit(0);
 
91
                        }
 
92
                }
 
93
        }
 
94
}
 
95