def pillow_operation_api(request): pillow_name = request.POST["pillow_name"] operation = request.POST["operation"] try: pillow_config = get_pillow_config_by_name(pillow_name) pillow = pillow_config.get_instance() except PillowNotFoundError: pillow_config = None pillow = None def get_response(error=None): response = { 'pillow_name': pillow_name, 'operation': operation, 'success': error is None, 'message': error, } if pillow_config: response.update(get_pillow_json(pillow_config)) return JsonResponse(response) if pillow: try: if operation == 'refresh': return get_response() except Exception as e: return get_response(str(e)) else: return get_response("No pillow found with name '{}'".format(pillow_name))
def pillow_operation_api(request): pillow_name = request.POST["pillow_name"] operation = request.POST["operation"] try: pillow_config = get_pillow_config_by_name(pillow_name) pillow = pillow_config.get_instance() except PillowNotFoundError: pillow_config = None pillow = None def get_response(error=None): response = { 'pillow_name': pillow_name, 'operation': operation, 'success': error is None, 'message': error, } if pillow_config: response.update(get_pillow_json(pillow_config)) return json_response(response) if pillow: try: if operation == 'refresh': return get_response() except Exception as e: return get_response(str(e)) else: return get_response("No pillow found with name '{}'".format(pillow_name))
def pillow_operation_api(request): pillow_name = request.POST["pillow_name"] operation = request.POST["operation"] try: pillow_config = get_pillow_config_by_name(pillow_name) pillow = pillow_config.get_instance() except PillowNotFoundError: pillow_config = None pillow = None def get_response(error=None): response = { 'pillow_name': pillow_name, 'operation': operation, 'success': error is None, 'message': error, } response.update(pillow_supervisor_status(pillow_name)) if pillow_config: response.update(get_pillow_json(pillow_config)) return json_response(response) @any_toggle_enabled(SUPPORT) def reset_pillow(request): pillow.reset_checkpoint() if PillowtopSupervisorApi().restart_pillow(pillow_name): return get_response() else: return get_response("Checkpoint reset but failed to restart pillow. " "Restart manually to complete reset.") @any_toggle_enabled(SUPPORT) def start_pillow(request): if PillowtopSupervisorApi().start_pillow(pillow_name): return get_response() else: return get_response('Unknown error') @any_toggle_enabled(SUPPORT) def stop_pillow(request): if PillowtopSupervisorApi().stop_pillow(pillow_name): return get_response() else: return get_response('Unknown error') if pillow: try: if operation == 'reset_checkpoint': reset_pillow(request) if operation == 'start': start_pillow(request) if operation == 'stop': stop_pillow(request) if operation == 'refresh': return get_response() except SupervisorException as e: return get_response(str(e)) else: return get_response("No pillow found with name '{}'".format(pillow_name))
def migrate_legacy_pillow_by_name(migration_apps, pillow_name): if settings.UNIT_TESTING: return try: DjangoPillowCheckpoint = migration_apps.get_model('pillowtop', 'DjangoPillowCheckpoint') pillow_config = get_pillow_config_by_name(pillow_name) checkpoint_id = construct_checkpoint_doc_id_from_name(pillow_config.get_class().get_legacy_name()) legacy_checkpoint = CommCareCase.get_db().get(checkpoint_id) new_checkpoint = DjangoPillowCheckpoint( checkpoint_id=pillow_config.get_instance().checkpoint.checkpoint_id, sequence=legacy_checkpoint['seq'], old_sequence=legacy_checkpoint.get('old_seq', None) ) new_checkpoint.save() except Exception as e: logging.exception('Failed to update pillow checkpoint. {}'.format(e))
def migrate_legacy_pillow_by_name(migration_apps, pillow_name): if settings.UNIT_TESTING: return try: DjangoPillowCheckpoint = migration_apps.get_model( 'pillowtop', 'DjangoPillowCheckpoint') pillow_config = get_pillow_config_by_name(pillow_name) checkpoint_id = construct_checkpoint_doc_id_from_name( pillow_config.get_class().get_legacy_name()) legacy_checkpoint = CommCareCase.get_db().get(checkpoint_id) new_checkpoint = DjangoPillowCheckpoint( checkpoint_id=pillow_config.get_instance( ).checkpoint.checkpoint_id, sequence=legacy_checkpoint['seq'], old_sequence=legacy_checkpoint.get('old_seq', None)) new_checkpoint.save() except Exception as e: logging.exception('Failed to update pillow checkpoint. {}'.format(e))