Giter VIP home page Giter VIP logo

Comments (2)

andrew72ru avatar andrew72ru commented on August 30, 2024

@rpstevefysh, it's understandable and comes from the nature of PHP. While the server is working, the client is waiting. You don't have any performance issues, server overloads, or something like that — everything really works as expected.

What you do see is network communications. While the file is being uploaded from the Uploadcare server instance to the S3 bucket, you will not receive a response and can't do anything in the current PHP process.

My only advice here is to use a queue on your side to make this operation asynchronous. I know it requires some infrastructure changes (at least, you have to have a queue manager and run the command on a server), but those changes are not so big and pretty understandable. For example, you can use the Redis service to make a queue and a simple SystemD configuration to keep it working.

Then, you can make a few messages with parameters and send them to the bus. Something like

<?php

final class CopyToRemoteMessage()
{
    public function __construct(
        readonly private string $sourceUrl,
        readonly private string $target,
        readonly private string $targetPattern,
    ) {}

    // ... your getters for properties
}

as the message class, and simply make those messages in your controller and send them

$original_target_pattern = $folder . '/' . $newFilenameWithTimestamp;
/**
 * Do not forget to inject the MessageBusInterface into your controller.
 * After this call, your code will continue immediately, and a message will be processed in a consumer (see below).
 */
$this->bus->dispatch(new CopyToRemoteMessage($source_uuid, $target, $original_target_pattern));

And example consumer could be list this:

<?php

use Uploadcare\Api;

final class CopyToRemoteConsumer
{
    public function __construct(
        readonly private Api $api,
    ) {}

    public function __invoke(CopyToRemoteMessage $message): void
    {
        $this->api->file()->copyToRemoteStorage($message->getSourceUrl(), true, $message->getTarget(), $message->getTargetPattern());
    }
}

In this way, you'll have a controller method that answers in milliseconds and a background process related to the actual uploading process. Of course, it will not speed up the uploading process itself; you'll have to wait a bit before that file is really uploaded to the S3 storage and check its state, but this will give you flexibility in a matter of user experience.

Please check the Uploadcare PHP repository for examples of dependency injections and configurations.

from uploadcare-php.

rpstevefysh avatar rpstevefysh commented on August 30, 2024

Thank you for your response. This makes sense and we will pursue the queue options.

from uploadcare-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.