def start_routine( type, corpus=None ): """ If a routine does not exist, it will create it. If a routine exists and: 1. is OK, routine will be set to RIP; a new one will be created. 2. is PN, outine won't be overridden and Return the current routine, created or not. """ from sven.anta.models import Routine from django.db.models import Q logger.info( "starting '%s' routine on corpus '%s'[%s]" %( type, corpus.name, corpus.id ) ) # exclude finished, closed with error and closed tout-court. working_routines = Routine.objects.filter( Q( status=Routine.PENDING ) | Q( status=Routine.START ), corpus=corpus ).order_by("-start_date") if working_routines.count() > 0: logger.warning( "a routine [id:%s] on corpus '%s'[%s] has not been completed. We're busy..." %( working_routines[0].id, corpus.name, corpus.id ) ) return working_routines[0] # start new routine try: r = Routine( type=type, corpus=corpus, status=Routine.START ) r.save() except: logger.exception( "Cannot create a routine object on corpus '%s'[%s]" %( corpus.name, corpus.id ) ) return None return r
def start_routine( type, corpus=None ): """ If a routine does not exist, it will create it. If a routine exists and: 1. is OK, routine will be set to RIP; a new one will be created. 2. is PN, outine won't be overridden and Return the current routine, created or not. """ from sven.anta.models import Routine # exclude finished, closed with error and closed tout-court. try: r = Routine.objects.exclude( status="OK").exclude(status="CLO").exclude(status="RIP").filter( type=type, corpus=corpus ).order_by( "-pk")[0] except Exception, e: # normally List index out of range, create a brand new one r = Routine( type=type, corpus=corpus ) r.save() return r