def task(p, delay, origin, verb_name, *args, **kwargs): """ Verb API: queue up a new task. """ # remind me again why we can't do this? from antioch.core import tasks tasks.registertask.delay( user_id = p.caller.id, delay = str(delay), origin_id = str(origin.id), verb_name = str(verb_name), args = json.dumps(args), kwargs = json.dumps(kwargs), )
def save(self, obj): """ Save the provided model back into the database. """ obj_type = type(obj).__name__.lower() obj_id = obj.get_id() if(obj_type == 'object'): attribs = dict( name = obj._name, unique_name = ('f', 't')[obj._unique_name], owner_id = obj._owner_id, location_id = obj._location_id, ) elif(obj_type == 'verb'): attribs = dict( code = obj._code, filename = obj._filename, owner_id = obj._owner_id, origin_id = obj._origin_id, ability = ('f', 't')[obj._ability], method = ('f', 't')[obj._method], ) elif(obj_type == 'property'): def check(v): if(v is None): return False elif(v is ""): return False return True attribs = dict( name = obj._name, value = json.dumps(obj._value) if check(obj._value) else obj._value, owner_id = obj._owner_id, origin_id = obj._origin_id, type = obj._type, ) else: raise RuntimeError("Don't know how to save an object of type '%s'" % obj_type) if(obj_id): self.pool.runOperation(sql.build_update(obj_type, attribs, dict(id=obj_id))) else: attribs['id'] = sql.RAW('DEFAULT') result = self.pool.runQuery(sql.build_insert(obj_type, attribs) + ' RETURNING id') obj.set_id(result[0]['id']) object_key = '%s-%s' % (obj_type, obj.get_id()) if(object_key not in self.cache): self.cache[object_key] = obj
def flush(self): """ Clear and save the cache, and send all pending messages. """ self.cache.clear() self.cache._order = [] if(self.queue): with celery.app.default_connection() as conn: from kombu import Exchange, Queue unbound_exchange = Exchange('antioch', type = 'direct', auto_delete = False, durable = True, ) channel = conn.channel() exchange = unbound_exchange(channel) exchange.declare() for user, msg in self.queue: if not(user.is_connected_player()): log.debug("ignoring message for unconnected player %s" % user) continue queue_id = '-'.join([settings.USER_QUEUE, str(user.id)]) log.debug("flushing message to #%s: %s" % (queue_id, msg)) exchange.publish(exchange.Message(json.dumps(msg), content_type="application/json"), routing_key=queue_id)