Search in sources :

Example 1 with Log

use of im.actor.runtime.Log in project actor-platform by actorapp.

the class UploadTask method preStart.

@Override
public void preStart() {
    // Storage.isAlreadyInTemp(descriptor);
    alreadyInTemp = false;
    isWriteToDestProvider = Storage.isFsPersistent() && !alreadyInTemp;
    srcReference = Storage.fileFromDescriptor(descriptor);
    if (srcReference == null) {
        if (LOG) {
            Log.d(TAG, "Error during file reference creating");
        }
        reportError();
        return;
    }
    if (isWriteToDestProvider) {
        destReference = Storage.createTempFile();
        if (destReference == null) {
            if (LOG) {
                Log.w(TAG, "Error during file dest reference creating");
            }
            reportError();
            return;
        }
    }
    srcReference.openRead().flatMap(f -> {
        inputFile = f;
        if (isWriteToDestProvider) {
            return destReference.openWrite(srcReference.getSize());
        } else {
            return Promise.success(null);
        }
    }).flatMap(f -> {
        outputFile = f;
        crc32 = new CRC32();
        blocksCount = srcReference.getSize() / blockSize;
        if (srcReference.getSize() % blockSize != 0) {
            blocksCount++;
        }
        if (LOG) {
            Log.d(TAG, "Starting uploading " + blocksCount + " blocks");
            Log.d(TAG, "Requesting upload config...");
        }
        return api(new RequestGetFileUploadUrl(srcReference.getSize()));
    }).then(r -> {
        if (LOG) {
            Log.d(TAG, "Upload config loaded");
        }
        uploadConfig = r.getUploadKey();
        checkQueue();
    }).failure(e -> {
        if (LOG) {
            Log.w(TAG, "Error during initialization of upload");
        }
        reportError();
    });
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ActorCancellable(im.actor.runtime.actors.ActorCancellable) ResponseGetFileUploadUrl(im.actor.core.api.rpc.ResponseGetFileUploadUrl) ResponseCommitFileUpload(im.actor.core.api.rpc.ResponseCommitFileUpload) Promise(im.actor.runtime.promise.Promise) OutputFile(im.actor.runtime.files.OutputFile) RpcException(im.actor.core.network.RpcException) Storage(im.actor.runtime.Storage) ModuleActor(im.actor.core.modules.ModuleActor) RequestCommitFileUpload(im.actor.core.api.rpc.RequestCommitFileUpload) RequestGetFileUploadUrl(im.actor.core.api.rpc.RequestGetFileUploadUrl) HTTPResponse(im.actor.runtime.http.HTTPResponse) FileReference(im.actor.core.entity.FileReference) FileSystemReference(im.actor.runtime.files.FileSystemReference) InputFile(im.actor.runtime.files.InputFile) RpcCallback(im.actor.core.network.RpcCallback) ActorRef(im.actor.runtime.actors.ActorRef) Log(im.actor.runtime.Log) HTTP(im.actor.runtime.HTTP) HTTPError(im.actor.runtime.http.HTTPError) CRC32(im.actor.runtime.crypto.CRC32) RequestGetFileUploadPartUrl(im.actor.core.api.rpc.RequestGetFileUploadPartUrl) CRC32(im.actor.runtime.crypto.CRC32) RequestGetFileUploadUrl(im.actor.core.api.rpc.RequestGetFileUploadUrl)

Example 2 with Log

use of im.actor.runtime.Log in project actor-platform by actorapp.

the class UploadTask method uploadPart.

private void uploadPart(final int blockIndex, final byte[] data, final int attempt) {
    api(new RequestGetFileUploadPartUrl(blockIndex, blockSize, uploadConfig)).flatMap(r -> HTTP.putMethod(r.getUrl(), data)).then(r -> {
        if (LOG) {
            Log.d(TAG, "Block #" + blockIndex + " uploaded");
        }
        uploadCount--;
        uploaded++;
        reportProgress(uploaded / (float) blocksCount);
        checkQueue();
    }).failure(e -> {
        if (e instanceof HTTPError) {
            HTTPError httpError = (HTTPError) e;
            if ((httpError.getErrorCode() >= 500 && httpError.getErrorCode() < 600) || httpError.getErrorCode() == 0) {
                // Is Server Error or unknown error
                int retryInSecs = DEFAULT_RETRY;
                if (LOG) {
                    Log.w(TAG, "Block #" + blockIndex + " upload error #" + httpError.getErrorCode() + " trying again in " + retryInSecs + " sec, attempt #" + (attempt + 1));
                }
                schedule(new Retry(blockIndex, data, attempt + 1), retryInSecs * 1000L);
                return;
            }
            if (LOG) {
                Log.w(TAG, "Block #" + blockIndex + " upload failure");
            }
            reportError();
        }
    });
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ActorCancellable(im.actor.runtime.actors.ActorCancellable) ResponseGetFileUploadUrl(im.actor.core.api.rpc.ResponseGetFileUploadUrl) ResponseCommitFileUpload(im.actor.core.api.rpc.ResponseCommitFileUpload) Promise(im.actor.runtime.promise.Promise) OutputFile(im.actor.runtime.files.OutputFile) RpcException(im.actor.core.network.RpcException) Storage(im.actor.runtime.Storage) ModuleActor(im.actor.core.modules.ModuleActor) RequestCommitFileUpload(im.actor.core.api.rpc.RequestCommitFileUpload) RequestGetFileUploadUrl(im.actor.core.api.rpc.RequestGetFileUploadUrl) HTTPResponse(im.actor.runtime.http.HTTPResponse) FileReference(im.actor.core.entity.FileReference) FileSystemReference(im.actor.runtime.files.FileSystemReference) InputFile(im.actor.runtime.files.InputFile) RpcCallback(im.actor.core.network.RpcCallback) ActorRef(im.actor.runtime.actors.ActorRef) Log(im.actor.runtime.Log) HTTP(im.actor.runtime.HTTP) HTTPError(im.actor.runtime.http.HTTPError) CRC32(im.actor.runtime.crypto.CRC32) RequestGetFileUploadPartUrl(im.actor.core.api.rpc.RequestGetFileUploadPartUrl) HTTPError(im.actor.runtime.http.HTTPError) RequestGetFileUploadPartUrl(im.actor.core.api.rpc.RequestGetFileUploadPartUrl)

Aggregations

RequestCommitFileUpload (im.actor.core.api.rpc.RequestCommitFileUpload)2 RequestGetFileUploadPartUrl (im.actor.core.api.rpc.RequestGetFileUploadPartUrl)2 RequestGetFileUploadUrl (im.actor.core.api.rpc.RequestGetFileUploadUrl)2 ResponseCommitFileUpload (im.actor.core.api.rpc.ResponseCommitFileUpload)2 ResponseGetFileUploadUrl (im.actor.core.api.rpc.ResponseGetFileUploadUrl)2 FileReference (im.actor.core.entity.FileReference)2 ModuleActor (im.actor.core.modules.ModuleActor)2 ModuleContext (im.actor.core.modules.ModuleContext)2 RpcCallback (im.actor.core.network.RpcCallback)2 RpcException (im.actor.core.network.RpcException)2 HTTP (im.actor.runtime.HTTP)2 Log (im.actor.runtime.Log)2 Storage (im.actor.runtime.Storage)2 ActorCancellable (im.actor.runtime.actors.ActorCancellable)2 ActorRef (im.actor.runtime.actors.ActorRef)2 CRC32 (im.actor.runtime.crypto.CRC32)2 FileSystemReference (im.actor.runtime.files.FileSystemReference)2 InputFile (im.actor.runtime.files.InputFile)2 OutputFile (im.actor.runtime.files.OutputFile)2 HTTPError (im.actor.runtime.http.HTTPError)2