<s> """<STR_LIT>""" <EOL> from django . core . management . base import BaseCommand <EOL> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from core . domains import setup_domain <EOL> from optparse import make_option <EOL> import multiprocessing <EOL> import logging <EOL> import logging . handlers <EOL> import signal <EOL> import time <EOL> import sys <EOL> LOG_LEVELS = { '<STR_LIT>' : logging . DEBUG , <EOL> '<STR_LIT:info>' : logging . INFO , <EOL> '<STR_LIT>' : logging . WARNING , <EOL> '<STR_LIT:error>' : logging . ERROR , <EOL> '<STR_LIT>' : logging . CRITICAL } <EOL> cronlogger = logging . getLogger ( '<STR_LIT>' ) <EOL> cronlogger . setLevel ( logging . DEBUG ) <EOL> class CronJob ( multiprocessing . Process ) : <EOL> "<STR_LIT>" <EOL> job = None <EOL> database = '<STR_LIT:default>' <EOL> priority = <NUM_LIT:5> <EOL> def __init__ ( self , job , database = '<STR_LIT:default>' , priority = <NUM_LIT:5> , * args , ** kwargs ) : <EOL> super ( CronJob , self ) . __init__ ( * args , ** kwargs ) <EOL> self . job = job <EOL> self . priority = priority <EOL> self . database = database <EOL> self . _stopped = False <EOL> def sigterm ( self , * args , ** kwargs ) : <EOL> cronlogger = logging . getLogger ( '<STR_LIT>' ) <EOL> if self . _stopped : <EOL> cronlogger . critical ( '<STR_LIT>' + unicode ( self ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> self . _stopped = True <EOL> cronlogger . warning ( '<STR_LIT>' + unicode ( self ) ) <EOL> raise KeyboardInterrupt ( ) <EOL> def run ( self ) : <EOL> signal . signal ( signal . SIGTERM , self . sigterm ) <EOL> setup_domain ( self . database ) <EOL> cronlogger = logging . getLogger ( '<STR_LIT>' ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self ) ) <EOL> try : <EOL> self . job ( ) <EOL> except KeyboardInterrupt : <EOL> self . _stopped = True <EOL> except SystemExit : <EOL> self . _stopped = True <EOL> except : <EOL> import traceback <EOL> from hardtree import core <EOL> from django . core . mail import mail_admins <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> subject = "<STR_LIT>" + unicode ( self ) + "<STR_LIT>" + unicode ( exc_type ) + "<STR_LIT:U+0020>" + unicode ( exc_value ) <EOL> body = subject + "<STR_LIT>" <EOL> body += unicode ( core . __file__ ) + "<STR_LIT>" <EOL> for s in traceback . format_tb ( exc_traceback ) : <EOL> body += s + '<STR_LIT:\n>' <EOL> cronlogger . error ( subject + '<STR_LIT:\n>' + body ) <EOL> mail_admins ( subject , body ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' + unicode ( self . database ) + '<STR_LIT>' + unicode ( self . job ) <EOL> def __unicode__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> class CronRunner ( ) : <EOL> "<STR_LIT>" <EOL> pool = [ ] <EOL> queue = [ ] <EOL> jobs = [ ] <EOL> sleeptime = <NUM_LIT> <EOL> cycle = <NUM_LIT:1> <EOL> _stopped = False <EOL> def __init__ ( self , databases = None , noloop = False , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> if databases is None : <EOL> databases = [ ] <EOL> signal . signal ( signal . SIGTERM , self . stop ) <EOL> self . databases = databases or [ ] <EOL> self . jobs = [ ] <EOL> self . sleeptime = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> self . priority_high = getattr ( <EOL> settings , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . priority_low = getattr ( settings , '<STR_LIT>' , <NUM_LIT:3> ) <EOL> self . qualify_high = getattr ( settings , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . qualify_run = getattr ( <EOL> settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> self . poolsize = getattr ( settings , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . softkill = getattr ( settings , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . hardkill = getattr ( settings , '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> self . gracewait = getattr ( settings , '<STR_LIT>' , <NUM_LIT:5> ) <EOL> self . noloop = noloop <EOL> for module in settings . INSTALLED_APPS : <EOL> import_name = str ( <EOL> module ) + "<STR_LIT:.>" + settings . HARDTREE_MODULE_IDENTIFIER <EOL> try : <EOL> hmodule = __import__ ( import_name , fromlist = [ str ( module ) ] ) <EOL> self . jobs . extend ( hmodule . CRON ) <EOL> except ImportError : <EOL> pass <EOL> except AttributeError : <EOL> pass <EOL> if not self . databases : <EOL> self . databases = [ db for db in settings . DATABASES ] <EOL> cronlogger . info ( '<STR_LIT>' ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . databases ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . sleeptime ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . priority_high ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . priority_low ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . qualify_high ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . poolsize ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . softkill ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . hardkill ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . gracewait ) ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . noloop ) ) <EOL> def add_jobs ( self ) : <EOL> "<STR_LIT>" <EOL> cronlogger . info ( <EOL> '<STR_LIT>' + unicode ( len ( self . jobs ) ) + '<STR_LIT>' ) <EOL> for db in self . databases : <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( db ) ) <EOL> cache_key = '<STR_LIT>' + db + '<STR_LIT>' <EOL> last_accessed = cache . get ( cache_key ) <EOL> if last_accessed : <EOL> if last_accessed >= time . time ( ) - int ( self . qualify_run ) : <EOL> for job in self . jobs : <EOL> cron = CronJob ( job , db , self . priority_low ) <EOL> self . queue . append ( cron ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( cron ) ) <EOL> else : <EOL> cronlogger . debug ( <EOL> '<STR_LIT>' + unicode ( db ) + '<STR_LIT>' + unicode ( self . qualify_run ) ) <EOL> else : <EOL> cronlogger . debug ( <EOL> '<STR_LIT>' + unicode ( db ) + '<STR_LIT>' ) <EOL> cronlogger . debug ( '<STR_LIT>' + unicode ( self . jobs ) ) <EOL> def start ( self ) : <EOL> "<STR_LIT>" <EOL> self . add_jobs ( ) <EOL> try : <EOL> while not self . _stopped : <EOL> if len ( self . pool ) < self . poolsize : <EOL> while len ( self . queue ) > <NUM_LIT:0> and len ( self . pool ) < self . poolsize : <EOL> cron = self . queue . pop ( ) <EOL> self . pool . append ( cron ) <EOL> if '<STR_LIT>' % ( cron . database ) in cache : <EOL> last_accessed = cache . get ( <EOL> '<STR_LIT>' % ( cron . database ) ) <EOL> if last_accessed > ( time . time ( ) - int ( self . qualify_high ) ) : <EOL> cron . priority = self . priority_high <EOL> cronlogger . debug ( <EOL> '<STR_LIT>' + unicode ( cron ) ) <EOL> cron . start ( ) <EOL> if len ( self . queue ) == <NUM_LIT:0> : <EOL> cronlogger . info ( <EOL> '<STR_LIT>' + unicode ( self . cycle ) + '<STR_LIT>' ) <EOL> self . cycle += <NUM_LIT:1> <EOL> if self . noloop : <EOL> self . _stopped = True <EOL> else : <EOL> self . add_jobs ( ) <EOL> time . sleep ( self . sleeptime ) <EOL> cronlogger . debug ( "<STR_LIT>" + unicode ( len ( self . pool ) ) ) <EOL> for cron in self . pool : <EOL> cronlogger . debug ( "<STR_LIT>" + unicode ( cron ) + '<STR_LIT>' + <EOL> unicode ( cron . priority ) + '<STR_LIT>' + unicode ( cron . is_alive ( ) ) ) <EOL> if not cron . is_alive ( ) : <EOL> self . pool . remove ( cron ) <EOL> continue <EOL> if cron . priority == self . softkill : <EOL> cron . terminate ( ) <EOL> elif cron . priority <= self . hardkill : <EOL> cron . terminate ( ) <EOL> cron . priority -= <NUM_LIT:1> <EOL> time . sleep ( self . sleeptime ) <EOL> except KeyboardInterrupt : <EOL> self . stop ( ) <EOL> def stop ( self , * args , ** kwargs ) : <EOL> cronlogger . info ( '<STR_LIT>' ) <EOL> self . _stopped = True <EOL> for cron in self . pool : <EOL> while cron . is_alive ( ) : <EOL> cron . terminate ( ) <EOL> time . sleep ( self . gracewait ) <EOL> cronlogger . debug ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> class Command ( BaseCommand ) : <EOL> args = '<STR_LIT>' <EOL> help = '<STR_LIT>' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = '<STR_LIT:info>' , <EOL> choices = [ i for i in LOG_LEVELS ] , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> def handle ( self , * args , ** options ) : <EOL> loghandler = logging . handlers . RotatingFileHandler ( <EOL> options . get ( '<STR_LIT>' ) , maxBytes = <NUM_LIT:100> * <NUM_LIT> * <NUM_LIT> , backupCount = <NUM_LIT:5> ) <EOL> formatter = logging . Formatter ( <EOL> "<STR_LIT>" ) <EOL> loghandler . setFormatter ( formatter ) <EOL> loghandler . setLevel ( LOG_LEVELS [ options . get ( '<STR_LIT>' ) ] ) <EOL> cronlogger . setLevel ( LOG_LEVELS [ options . get ( '<STR_LIT>' ) ] ) <EOL> cronlogger . addHandler ( loghandler ) <EOL> self . runner = CronRunner ( args , noloop = options . get ( '<STR_LIT>' ) ) <EOL> self . runner . start ( ) </s>
