def clean_model(self, model, days, count, verbose=False): from uliweb import functions import time import logging import types from uliweb.orm import Begin, Commit, Rollback log = logging.getLogger(__name__) if verbose: print('Clean {}, days={}, count={} ...'.format(model, days, count)) M = functions.get_model(model) if hasattr(M, 'clear_data'): b = time.time() t = 0 Begin() try: ret = M.clear_data(days, count) Commit() except Exception as e: Rollback() log.exception(e) return if isinstance(ret, types.GeneratorType): while 1: Begin() try: n = ret.next() if n == None: break t += n Commit() except StopIteration: break except Exception as e: Rollback() log.exception(e) break else: t = ret print( 'Used {} seconds to clean the {}, total records is {}'.format( time.time() - b, model, t)) else: print('There is no clear_data() function defined for {}'.format( model))
def test(i): from uliweb.orm import get_model from uliweb.orm import Begin, Commit, Rollback try: Begin() TaskDB = get_model('workflow_task') cond = None query = TaskDB.filter(cond).limit(10) ids = [task_obj.id for task_obj in query] Commit(close=True) except: Rollback() raise from uliweb.orm import print_pool_status print_pool_status() from random import randint if randint(1,2) == 1: test2() else: import gevent gevent.sleep(0.5) print i return
def test_manual_rollback(): from uliweb.orm import Begin, Commit, Rollback Begin() Blog = functions.get_model('blog') b = Blog(title='test_add', content='test_add') b.save() Rollback() return 'ok'
def start_job(job, now=None): from uliweb.utils import date now = now or date.now() log.info(u'Starting job [{title} -- {time}] at {now}'.format( title=job.title, now=now, time=job.time)) Task = functions.get_model('cron_task') Begin() try: parent = functions.AsyncCommand( '', title=job.title, startup_time=now, category='cron', message_source='l', timeout=job.timeout, #todo add correlation to job ) commands = [] ids = {} #process task for row in Task.filter(Task.c.cron_job == job.id): #process async task d = parse_command(row) c = functions.AsyncCommand( title=row.label, category='cron', message_source='l', correlation=job.id, queue=(row.queue or 'default').split(','), timeout=row.timeout or None, # correlation_link='/async_task/view/{0}?next=/cron/{1}'.format(job.id, obj.id) **d) commands.append((c, row.depend_tasks)) ids[row.id] = c.task_id parent.add_child(c) #fix depends for c, depends in commands: _d = [ids[x] for x in depends] c.depend_tasks = _d functions.call_async(parent) job.instances.add(parent.task_id) Commit() except Exception as e: Rollback() log.exception(e) raise
def save(self, **kwargs): from uliweb.orm import Begin, Commit, Rollback Begin() try: self.task.update(**kwargs) self._new_data = {} ret = self.task.save(version=True, saved=self._saved) Commit() if self._new_data: self.on_changed(self._new_data) except Exception as e: Rollback() self.log.exception(e) raise
def load_ready_tasks(self, queue): from uliweb.orm import Begin, Commit, Rollback, Reset from gevent.queue import Full import gevent if self.queue.full(): gevent.sleep(0.3) return Begin() try: cond = self.task_model.c.state == 2 # READY cond = (self.task_model.c.async_status == 0 or self.task_model.c.async_status == None) & cond query = self.task_model.filter(cond) query = query.order_by(self.task_model.c.async_deliver_date.asc()) query = query.limit(self.limit) isFull = False isEmpty = True for item in query: self.tid = self.tid + 1 try: queue.put_nowait([self.tid, item.id]) item.update(async_status=1) item.save() isEmpty = False except Full: isFull = True break Commit() if isEmpty and self.debug: self.prints(">>> [DEBUG] No tasks found %s" % self.gettimestamp()) if isFull: gevent.sleep(0.3) if isEmpty: gevent.sleep(0.3) except: Rollback() import traceback traceback.print_exc()
def on_exception(self, e): Rollback()