run_streams.py 1.6 KB
Newer Older
1 2 3 4 5 6 7
from django.core.management.base import NoArgsCommand
from streams.models import *

class Command(NoArgsCommand):
    help = 'check for streams that need to be executed and execute them'
    option_list = NoArgsCommand.option_list
    def handle_noargs(self, **options):
Janez K's avatar
Janez K committed
8 9 10 11 12 13
        import time
        self.stdout.write("Working on streams...")
        self.stdout.flush()
        while True:
            streams = Stream.objects.filter(active=True)
            for stream in streams:
Janez K's avatar
Janez K committed
14 15
                #self.stdout.write(u"\nChecking stream "+unicode(stream)+"...\n")
                #self.stdout.flush()
Janez K's avatar
Janez K committed
16 17 18 19 20 21 22 23
                #preverimo ce je ze dost casa pretekl
                import django
                now = django.utils.timezone.now()
                delta = now - stream.last_executed
                delta_seconds = delta.seconds + delta.days * 86400
                if delta_seconds > stream.period:
                    stream.last_executed = now
                    stream.save()
Janez K's avatar
Janez K committed
24 25 26 27 28 29 30 31 32 33
                    self.stdout.write(u"Executing "+unicode(stream)+"...")
                    self.stdout.flush()
                    try:
                        stream.execute()
                    except:
                        import traceback
                        self.stdout.write("\n ERROR in executing stream:\n")
                        traceback.print_exc(file=self.stdout)
                    self.stdout.write("done!\n")
                    self.stdout.flush()
Janez K's avatar
Janez K committed
34 35 36 37
                    #print stream.execute()
            time.sleep(1)
            #self.stdout.write(".")
            #self.stdout.flush()
Janez K's avatar
Janez K committed
38