def process_sync(sync_id): sync = Sync.get(sync_id) if not sync: return if Transfer.find_one({'sync_id': sync['_id'], 'finished': None}): set_retry(sync) return src = get_uris(**sync['src']) if not src: set_retry(sync) return dst = get_uris(**sync['dst']) if not dst: set_retry(sync) return dst = dst[0] parameters = { 'exclusions': sync.get('exclusions'), 'delete': sync.get('delete'), } is_mount = (sync['dst'].get('uuid') or sync['src'].get('uuid')) is not None transfer_id = Transfer.add(src, dst, sync_id=sync['_id'], parameters=parameters, is_mount=is_mount) logger.info('added transfer %s to %s', src, dst) sync['transfer_id'] = transfer_id sync['processed'] = datetime.utcnow() sync['reserved'] = datetime.utcnow() + timedelta(hours=sync['recurrence']) Sync.save(sync, safe=True)
def reset_sync(): data = request.json if not data.get('id'): return jsonify(error='missing id') Sync.update({'_id': ObjectId(data['id'])}, {'$set': {'reserved': None}}, safe=True) return jsonify(result=True)
def run(): sync_timeout = Settings.get_settings('sync')['sync_timeout'] for sync in Sync.find({'$or': [ {'reserved': None}, {'reserved': {'$lt': datetime.utcnow()}}, ]}): if not validate_sync(sync): continue target = '%s.workers.sync.process_sync' % settings.PACKAGE_NAME get_factory().add(target=target, args=(sync['_id'],), timeout=sync_timeout)
def list_syncs(): now = datetime.utcnow() items = [] for res in Sync.find(sort=[('processed', DESCENDING)]): res['name'] = '%s to %s' % (_get_params_str(res['src']), _get_params_str(res['dst'])) if res.get('transfer_id'): transfer = Transfer.find_one({'_id': ObjectId(res['transfer_id'])}) else: transfer = None res['transfer'] = transfer or {} if not transfer: res['status'] = 'pending' elif transfer['finished'] and transfer['finished'] > now - timedelta(hours=res['recurrence'] + 24): res['status'] = 'ok' else: res['status'] = 'queued' items.append(res) return serialize({'result': items})
def run(): sync_timeout = Settings.get_settings('sync')['sync_timeout'] for sync in Sync.find({ '$or': [ { 'reserved': None }, { 'reserved': { '$lt': datetime.utcnow() } }, ] }): if not validate_sync(sync): continue target = '%s.workers.sync.process_sync' % settings.PACKAGE_NAME get_factory().add(target=target, args=(sync['_id'], ), timeout=sync_timeout)
def set_retry(sync): delta = Settings.get_settings('sync')['sync_retry_delta'] sync['reserved'] = datetime.utcnow() + timedelta(minutes=delta) Sync.save(sync, safe=True)
def remove_sync(): data = request.json if not data.get('id'): return jsonify(error='missing id') Sync.remove({'_id': ObjectId(data['id'])}) return jsonify(result=True)
for key, val in res[type].items(): if key == 'user': res[type][key] = ObjectId(val) for key in ('hour_begin', 'hour_end'): res[key] = int(data[key]) if data.get(key) else None return res @app.route('/sync/create', methods=['POST', 'OPTIONS']) @crossdomain(origin='*') def create_sync(): data = request.json try: sync = _get_sync(data) except SyncError, e: return jsonify(error=str(e)) if Sync.find_one(sync): return jsonify(error='sync already exists') Sync.insert(sync, safe=True) return jsonify(result=True) def _get_params_str(params): base = '' user_id = params.get('user') if user_id: user = get_user(user_id) if user: base = user['name'] if not base: base = params.get('hwaddr') or params.get('uuid')