~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/TeeJee.Process.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:
529
529
 
530
530
                process_set_priority (procID, 5);
531
531
        }
532
 
 
533
 
        public class TimeoutCounter : GLib.Object {
534
 
 
535
 
                public bool active = false;
536
 
                public string process_to_kill = "";
537
 
                public int seconds_to_wait = 30;
538
 
                public bool exit_app = false;
539
 
                
540
 
                public void kill_process_on_timeout(
541
 
                        string process_to_kill, int seconds_to_wait = 20, bool exit_app = false){
542
 
 
543
 
                        this.process_to_kill = process_to_kill;
544
 
                        this.seconds_to_wait = seconds_to_wait;
545
 
                        this.exit_app = exit_app;
546
 
                                
547
 
                        try {
548
 
                                active = true;
549
 
                                Thread.create<void> (start_counter_thread, true);
550
 
                        }
551
 
                        catch (Error e) {
552
 
                                log_error (e.message);
553
 
                        }
554
 
                }
555
 
 
556
 
                public void exit_on_timeout(int seconds_to_wait = 20){
557
 
                        this.process_to_kill = "";
558
 
                        this.seconds_to_wait = seconds_to_wait;
559
 
                        this.exit_app = true;
560
 
                                
561
 
                        try {
562
 
                                active = true;
563
 
                                Thread.create<void> (start_counter_thread, true);
564
 
                        }
565
 
                        catch (Error e) {
566
 
                                log_error (e.message);
567
 
                        }
568
 
                }
569
 
 
570
 
                public void stop(){
571
 
                        active = false;
572
 
                }
573
 
                
574
 
                public void start_counter_thread(){
575
 
                        int secs = 0;
576
 
                        
577
 
                        while (active && (secs < seconds_to_wait)){
578
 
                                Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 1000);
579
 
                                secs += 1;
580
 
                        }
581
 
 
582
 
                        if (active){
583
 
                                active = false;
584
 
                                stdout.printf("\n");
585
 
 
586
 
                                if (process_to_kill.length > 0){
587
 
                                        Posix.system("killall " + process_to_kill);
588
 
                                        stderr.printf("\n[timeout] Killed process" + ": %s\n".printf(process_to_kill));
589
 
                                }
590
 
 
591
 
                                if (exit_app){
592
 
                                        stderr.printf("\n[timeout] Exit application\n");
593
 
                                        exit(0);
594
 
                                }
595
 
                        }
596
 
                }
597
 
        }
598
 
 
599
 
        public class AppLock : GLib.Object {
600
 
                public string lock_file = "";
601
 
                public string lock_message = "";
602
 
                
603
 
                public bool create(string app_name, string message){
604
 
 
605
 
                        var lock_dir = "/var/run/lock/%s".printf(app_name);
606
 
                        dir_create(lock_dir);
607
 
                        lock_file = path_combine(lock_dir, "lock");
608
 
                        
609
 
                        try{
610
 
                                var file = File.new_for_path(lock_file);
611
 
                                if (file.query_exists()) {
612
 
 
613
 
                                        string txt = file_read(lock_file);
614
 
                                        string process_id = txt.split(";")[0].strip();
615
 
                                        lock_message = txt.split(";")[1].strip();
616
 
                                        long pid = long.parse(process_id);
617
 
 
618
 
                                        if (process_is_running(pid)){
619
 
                                                log_msg(_("Another instance of this application is running")
620
 
                                                        + " (PID=%ld)".printf(pid));
621
 
                                                return false;
622
 
                                        }
623
 
                                        else{
624
 
                                                log_msg(_("[Warning] Deleted invalid lock"));
625
 
                                                file.delete();
626
 
                                                write_lock_file(message);
627
 
                                                return true;
628
 
                                        }
629
 
                                }
630
 
                                else{
631
 
                                        write_lock_file(message);
632
 
                                        return true;
633
 
                                }
634
 
                        }
635
 
                        catch (Error e) {
636
 
                                log_error (e.message);
637
 
                                return false;
638
 
                        }
639
 
                }
640
 
 
641
 
                private void write_lock_file(string message){
642
 
                        string current_pid = ((long) Posix.getpid()).to_string();
643
 
                        file_write(lock_file, "%s;%s".printf(current_pid, message));
644
 
                }
645
 
                
646
 
                public void remove(){
647
 
                        try{
648
 
                                var file = File.new_for_path (lock_file);
649
 
                                if (file.query_exists()) {
650
 
                                        file.delete();
651
 
                                }
652
 
                        }
653
 
                        catch (Error e) {
654
 
                                log_error (e.message);
655
 
                        }
656
 
                }
657
 
 
658
 
        }
659
532
}