<s> import os <EOL> import logging <EOL> import time <EOL> import datetime <EOL> from itertools import chain <EOL> from django . conf import settings <EOL> from django_extensions . management . jobs import HourlyJob <EOL> from django_vcs_watch . models import Repository , Commit , Feed <EOL> from django_vcs_watch . settings import DELAY_BETWEEN_UPDATE_CHECK <EOL> from django_vcs_watch . utils import mongo <EOL> class Job ( HourlyJob ) : <EOL> help = "<STR_LIT>" <EOL> def execute ( self ) : <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> pidfile = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . exists ( pidfile ) : <EOL> pid = int ( open ( pidfile ) . read ( ) ) <EOL> if os . path . exists ( '<STR_LIT>' % pid ) : <EOL> log . warning ( '<STR_LIT>' % ( pid , pidfile ) ) <EOL> return <EOL> f = open ( pidfile , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( str ( os . getpid ( ) ) ) <EOL> finally : <EOL> f . close ( ) <EOL> try : <EOL> while True : <EOL> if self . update_repositories ( ) : <EOL> self . update_feeds ( ) <EOL> time . sleep ( DELAY_BETWEEN_UPDATE_CHECK ) <EOL> finally : <EOL> os . remove ( pidfile ) <EOL> def update_repositories ( self ) : <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> new_reps = Repository . objects . find ( { '<STR_LIT>' : None } ) <EOL> reps_to_update = Repository . objects . find ( { '<STR_LIT>' : { '<STR_LIT>' : datetime . datetime . utcnow ( ) } } ) <EOL> num_commits_before = Commit . objects . count ( ) <EOL> for rep in chain ( new_reps , reps_to_update ) : <EOL> log . debug ( '<STR_LIT>' % rep . url ) <EOL> try : <EOL> rep . update ( ) <EOL> except Exception , e : <EOL> log . exception ( "<STR_LIT>" % rep ) <EOL> Commit . objects . ensure_index ( [ ( '<STR_LIT:date>' , <NUM_LIT:1> ) ] ) <EOL> return num_commits_before != Commit . objects . count ( ) <EOL> def update_feeds ( self ) : <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> feeds = Feed . objects . find ( ) <EOL> for feed in feeds : <EOL> log . debug ( '<STR_LIT>' % feed . _id ) <EOL> try : <EOL> feed . update ( ) <EOL> except Exception , e : <EOL> log . exception ( "<STR_LIT>" % feed . _id ) </s>
