~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/tools/watchdog.py

Merge of my work on liblarch newbase and all the backends ported to liblarch
(which mainly means porting the datastore).
One failing test, will check it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# -----------------------------------------------------------------------------
 
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
 
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify it under
 
7
# the terms of the GNU General Public License as published by the Free Software
 
8
# Foundation, either version 3 of the License, or (at your option) any later
 
9
# version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
# -----------------------------------------------------------------------------
 
19
import threading
 
20
 
 
21
class Watchdog(object):
 
22
    '''
 
23
    a simple thread-safe watchdog.
 
24
    usage:
 
25
    with Watchdod(timeout, error_function):
 
26
        #do something
 
27
    '''
 
28
 
 
29
    def __init__(self, timeout, error_function):
 
30
        '''
 
31
        Just sets the timeout and the function to execute when an error occours
 
32
 
 
33
        @param timeout: timeout in seconds
 
34
        @param error_function: what to execute in case the watchdog timer
 
35
                               triggers
 
36
        '''
 
37
        self.timeout = timeout
 
38
        self.error_function = error_function
 
39
 
 
40
    def __enter__(self):
 
41
        '''Starts the countdown'''
 
42
        self.timer = threading.Timer(self.timeout, self.error_function)
 
43
        self.timer.start()
 
44
 
 
45
    def __exit__(self, type, value, traceback):
 
46
        '''Aborts the countdown'''
 
47
        try:
 
48
            self.timer.cancel()
 
49
        except:
 
50
            pass
 
51
        if value == None:
 
52
            return True
 
53
        return False