ulg-cron.py 1.97 KB
Newer Older
1 2 3
#!/usr/bin/env python
#
# ULG - Universal Looking Glass
4
# (C) 2012 CZ.NIC, z.s.p.o.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# 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 3 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, see <http://www.gnu.org/licenses/>.


# Imports
21
import os, os.path, sys
22
import random
23
import time, datetime
24
import pickle
25
import re
26 27 28 29 30 31 32 33

import config
import defaults

import ulgmodel

### ULG cron script

34 35
SESSION_FILE_REGEX='^ulg-.*\.session$'

36 37 38 39
class ULGCron:
    def __init__(self):
        pass

40 41 42 43 44
    def rescanRouters(self):
        for r in config.routers:
            r.rescanHook()

    def clearSessions(self):
45 46 47 48
        sre = re.compile(SESSION_FILE_REGEX)
        for file in os.listdir(defaults.session_dir):
            if sre.match(file):
                fp = defaults.session_dir+'/'+file
49 50 51 52 53 54 55 56 57 58

                now = datetime.datetime.now()
                if(datetime.datetime.fromtimestamp(os.path.getmtime(fp)) < now+datetime.timedelta(hours=-1)):
                    ulgmodel.log('Removing file '+fp)
                    try:
                        os.unlink(fp)
                    except OSError as e:
                        ulgmodel.log('Error while removing file '+fp+' '+str(e))
                else:
                    ulgmodel.log('Not removing file '+fp+' because it is not at least 1 hour old.')
59

60
    def run(self):
61
        ulgmodel.log('ULG cron run.')
62 63
        self.rescanRouters()
        self.clearSessions()
64
        ulgmodel.log('ULG cron finished.')
65

66 67 68 69

# main

if __name__=="__main__":
70
    sys.exit(ULGCron().run())