예제 #1
0
from allura.lib import helpers as h

from .session import main_doc_session, main_orm_session
from .project import Project
import six

log = logging.getLogger(__name__)

# Collection definitions
ArtifactReferenceDoc = collection(
    str('artifact_reference'),
    main_doc_session,
    Field('_id', str),
    Field(
        'artifact_reference',
        dict(cls=S.Binary(),
             project_id=S.ObjectId(),
             app_config_id=S.ObjectId(),
             artifact_id=S.Anything(if_missing=None))),
    Field('references', [str], index=True),
    Index('artifact_reference.project_id'),  # used in ReindexCommand
)

ShortlinkDoc = collection(
    str('shortlink'),
    main_doc_session,
    Field('_id', S.ObjectId()),
    # index needed for from_artifact() and index_tasks.py:del_artifacts
    Field('ref_id', str, index=True),
    Field('project_id', S.ObjectId()),
    Field('app_config_id', S.ObjectId()),
예제 #2
0
class HTTPMessage(Document):
    missing_client = '-' * 50
    channel = ChannelProxy('chapman.event')

    class __mongometa__:
        name = 'chapman.http_message'
        session = doc_session
        indexes = [[('s.status', 1), ('s.pri', -1), ('s.ts_enqueue', 1),
                    ('s.q', 1)],
                   [('s.q', 1), ('s.status', 1), ('s.pri', -1),
                    ('s.ts_enqueue', 1)], [('tags', 1)]]

    _id = Field(int, if_missing=lambda: getrandbits(63))
    _data = Field('data', S.Binary(if_missing=bson.Binary('{}')))
    tags = Field('tags', [str])
    schedule = Field(
        's',
        dict(status=S.String(if_missing='ready'),
             ts_enqueue=S.DateTime(if_missing=datetime.utcnow),
             ts_reserve=S.DateTime(if_missing=None),
             ts_timeout=S.DateTime(if_missing=None),
             timeout=S.Int(if_missing=300),
             after=S.DateTime(if_missing=datetime.fromtimestamp(0)),
             q=S.String(if_missing='chapman.http'),
             pri=S.Int(if_missing=10),
             cli=S.String(if_missing=missing_client)))

    def __json__(self, request):
        return {self.url(request): self.data}

    @property
    def data(self):
        return json.loads(self._data)

    @data.setter
    def data(self, value):
        self._data = bson.Binary(json.dumps(value, default=util.default_json))

    def url(self, request):
        scheme = request.registry.settings.get('chapman.message_url_scheme',
                                               None)
        args = dict(message_id=self._id)
        if scheme is not None:
            args['_scheme'] = scheme
        return request.route_url('chapman.1_0.message', **args)

    @classmethod
    def new(cls,
            data,
            timeout=300,
            after=None,
            q='chapman.http',
            pri=10,
            tags=None):
        if tags is None:
            tags = []
        _data = bson.Binary(json.dumps(data, default=util.default_json))
        if after is None:
            after = datetime.utcnow()
        self = cls.make(
            dict(data=_data,
                 tags=tags,
                 s=dict(timeout=timeout, after=after, q=q, pri=pri)))
        self.m.insert()
        self.channel.pub('enqueue', self._id)
        return self

    @classmethod
    def reserve(cls, cli, queues):
        return cls._reserve(cli, {'$in': queues})

    @classmethod
    def _reserve(cls, cli, qspec):
        now = datetime.utcnow()
        self = cls.m.find_and_modify(
            {
                's.status': 'ready',
                's.q': qspec,
                's.after': {
                    '$lte': now
                }
            },
            sort=[('s.pri', -1), ('s.ts_enqueue', 1)],
            update={
                '$set': {
                    's.cli': cli,
                    's.status': 'reserved',
                    's.ts_reserve': now
                }
            },
            new=True)
        if self is not None and self.s.timeout:
            self.m.set(
                {'s.ts_timeout': now + timedelta(seconds=self.s.timeout)})
        return self
예제 #3
0
from ming.orm import session, mapper
from ming.orm import ForeignIdProperty, RelationProperty

from allura.lib import helpers as h

from .session import main_doc_session, main_orm_session
from .project import Project

log = logging.getLogger(__name__)

# Collection definitions
ArtifactReferenceDoc = collection(
    'artifact_reference', main_doc_session,
    Field('_id', str),
    Field('artifact_reference', dict(
            cls=S.Binary(),
            project_id=S.ObjectId(),
            app_config_id=S.ObjectId(),
            artifact_id=S.Anything(if_missing=None))),
    Field('references', [str], index=True),
    Index('artifact_reference.project_id'),  # used in ReindexCommand
)

ShortlinkDoc = collection(
    'shortlink', main_doc_session,
    Field('_id', S.ObjectId()),
    Field('ref_id', str, index=True),  # index needed for from_artifact() and index_tasks.py:del_artifacts
    Field('project_id', S.ObjectId()),
    Field('app_config_id', S.ObjectId()),
    Field('link', str),
    Field('url', str),