Example #1
0
def save_file(f):
    new_name = '%s_%s' % (utcnow().strftime('%Y%m%d%H%M%S'), f._name)
    if not os.path.exists(INCOMING_ROOT):
        os.makedirs(INCOMING_ROOT)
    full_path = os.path.join(INCOMING_ROOT, new_name)
    with open(full_path, 'wb') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    return new_name
Example #2
0
def save_file(f):
    new_name = '%s_%s' % (utcnow().strftime('%Y%m%d%H%M%S'), f._name)
    if not os.path.exists(INCOMING_ROOT):
        os.makedirs(INCOMING_ROOT)
    full_path = os.path.join(INCOMING_ROOT, new_name)
    with open(full_path, 'wb') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    return new_name 
Example #3
0
    def handle(self, **options):
        taskName = options['task']
        if not taskName: raise ValueError('required argument --task not specified')
        async = options['async']
        
        if async:
            D = Detacher()
            if sys.platform=='win32':
                if not options['detached']:
                    D.detach()
            else:
                D.detach()

            #if we're here we should be fully detached
        #if --task-testing was passed we use a test setup
        if options['testing']:
            from django.test.utils import setup_test_environment
            setup_test_environment()
        pid = os.getpid()
        logs = os.path.join(settings.ENVIRON_DIR,'logs')
        if not os.path.isdir(logs):
            os.makedirs(logs)
        log = IdentiFile(open(os.path.join(logs,'deferredtasks.out'),'a'),'%-5d' %pid)
        success = False
        print >> log, '##### task %s[%-5d] started' % (taskName,pid)
        try:
            if options['log_std']:
                #debug version everything get's written to the logfile as well
                class StringIOX(StringIO):
                    def write(self,s):
                        log.write(s)
                        StringIO.write(self,s)
                sys_stdout = StringIOX()
                sys_stderr = StringIOX()
            else:
                sys_stdout = StringIO()
                sys_stderr = StringIO()
            if not async:
                old_stdout = sys.stdout
                old_stderr = sys.stderr
            sys.stdout = sys_stdout
            sys.stderr = sys_stderr
            dtask = DeferredTask()
            dtask.start = utcnow()
            dtask.pid = pid
            dtask.progress = 0
            username = options.get('username','')
            dtask.username = decode(username) if username else ''
            dtask.status = 'initialized'


            # reset DB connection which might have been killed in detachment process
            # but close_connection deprecated in Django 1.8
            # seems to be working OK without this
            #db.close_connection()
            
            dtask.save()
            D = {}
            dtask.task = taskName
            dtask.save()
            exec 'from %s import %s as task' % tuple(taskName.rsplit('.',1)) in D
            task = D['task']
            args = options.get('args',())
            if args: args = decode(args)
            kwds = options.get('kwds',{})
            if kwds: kwds = decode(kwds)
            _success_url = kwds.pop('task_success_url', False)
            dtask.success_url = ''
            if _success_url and not '?pid=' in _success_url:
                if '?' in _success_url:
                    dtask.success_url = "%s&pid=%s" % (_success_url, dtask.pid)
                else:
                    dtask.success_url = "%s?pid=%s" % (_success_url, dtask.pid)
            dtask.args=repr(args)
            dtask.kwds=repr(kwds)
            dtask.hashcode = hashFunc(taskName,args,kwds)
            dtask.title = kwds.pop('task_title','')
            dtask.status = 'prepared'
            dtask.save()
            result = getattr(task,'_task_func',task)(*args,**kwds)
            dtask = DeferredTask.objects.get(pid=pid)
            dtask.result = result
            dtask.status = 'finished'
            dtask.progress = 100
            dtask.save()
            success = True
        except:
            traceback.print_exc()
            traceback.print_exc(file=log)
        finally:
            try:
                dtask = DeferredTask.objects.get(pid=pid)
                dtask.success = success
                dtask.finished = True
                dtask.status = 'suceeded' if success else 'failed'
                dtask.finish = utcnow()
                dtask.stdout = sys_stdout.getvalue()
                dtask.stderr = sys_stderr.getvalue()
                dtask.save()
                print >> log, '%s task %s[%-5d] %s' % (('!!!!!','#####')[success],taskName,pid,('failed!','ended OK.')[success])
            except:
                traceback.print_exc(file=log)
            finally:
                if not async:
                    sys.stdout = old_stdout
                    sys.stderr = old_stderr
Example #4
0
    def handle(self, **options):
        taskName = options['task']
        if not taskName:
            raise ValueError('required argument --task not specified')
        async = options['async']

        if async:
            D = Detacher()
            if sys.platform == 'win32':
                if not options['detached']:
                    D.detach()
            else:
                D.detach()

            #if we're here we should be fully detached
        #if --task-testing was passed we use a test setup
        if options['testing']:
            from django.test.utils import setup_test_environment
            setup_test_environment()
        pid = os.getpid()
        logs = os.path.join(settings.ENVIRON_DIR, 'logs')
        if not os.path.isdir(logs):
            os.makedirs(logs)
        log = IdentiFile(open(os.path.join(logs, 'deferredtasks.out'), 'a'),
                         '%-5d' % pid)
        success = False
        print >> log, '##### task %s[%-5d] started' % (taskName, pid)
        try:
            if options['log_std']:
                #debug version everything get's written to the logfile as well
                class StringIOX(StringIO):
                    def write(self, s):
                        log.write(s)
                        StringIO.write(self, s)

                sys_stdout = StringIOX()
                sys_stderr = StringIOX()
            else:
                sys_stdout = StringIO()
                sys_stderr = StringIO()
            if not async:
                old_stdout = sys.stdout
                old_stderr = sys.stderr
            sys.stdout = sys_stdout
            sys.stderr = sys_stderr
            dtask = DeferredTask()
            dtask.start = utcnow()
            dtask.pid = pid
            dtask.progress = 0
            username = options.get('username', '')
            dtask.username = decode(username) if username else ''
            dtask.status = 'initialized'

            # reset DB connection which might have been killed in detachment process
            # but close_connection deprecated in Django 1.8
            # seems to be working OK without this
            #db.close_connection()

            dtask.save()
            D = {}
            dtask.task = taskName
            dtask.save()
            exec 'from %s import %s as task' % tuple(taskName.rsplit('.',
                                                                     1)) in D
            task = D['task']
            args = options.get('args', ())
            if args: args = decode(args)
            kwds = options.get('kwds', {})
            if kwds: kwds = decode(kwds)
            _success_url = kwds.pop('task_success_url', False)
            dtask.success_url = ''
            if _success_url and not '?pid=' in _success_url:
                if '?' in _success_url:
                    dtask.success_url = "%s&pid=%s" % (_success_url, dtask.pid)
                else:
                    dtask.success_url = "%s?pid=%s" % (_success_url, dtask.pid)
            dtask.args = repr(args)
            dtask.kwds = repr(kwds)
            dtask.hashcode = hashFunc(taskName, args, kwds)
            dtask.title = kwds.pop('task_title', '')
            dtask.status = 'prepared'
            dtask.save()
            result = getattr(task, '_task_func', task)(*args, **kwds)
            dtask = DeferredTask.objects.get(pid=pid)
            dtask.result = result
            dtask.status = 'finished'
            dtask.progress = 100
            dtask.save()
            success = True
        except:
            traceback.print_exc()
            traceback.print_exc(file=log)
        finally:
            try:
                dtask = DeferredTask.objects.get(pid=pid)
                dtask.success = success
                dtask.finished = True
                dtask.status = 'suceeded' if success else 'failed'
                dtask.finish = utcnow()
                dtask.stdout = sys_stdout.getvalue()
                dtask.stderr = sys_stderr.getvalue()
                dtask.save()
                print >> log, '%s task %s[%-5d] %s' % (
                    ('!!!!!', '#####')[success], taskName, pid,
                    ('failed!', 'ended OK.')[success])
            except:
                traceback.print_exc(file=log)
            finally:
                if not async:
                    sys.stdout = old_stdout
                    sys.stderr = old_stderr