brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
319 stars 63 forks source link

Missing override path for "real-time" facades #8

Closed yangm97 closed 1 year ago

yangm97 commented 4 years ago
file_put_contents(/var/task/storage/framework/cache/facade-d6c8a1ed6fde286ae195e535517be856b15c1f39.php): failed to open stream: No such file or directory
mnapoli commented 4 years ago

Hi, have you tried upgrading to the latest version?

In the last version, I moved the cache directory in /tmp, which should solve the problem with all cache directories.

yangm97 commented 4 years ago

Unfortunately it seems like the facade cache doesn't make use of the cache directory setting and makes raw calls to storage_path() as seen here and here.

Do you think this needs to be fixed upstream instead?

mnapoli commented 4 years ago

I would think so yes (fix upstream) but it doesn't look like it will. I'm not sure what we can do though 🤔

yangm97 commented 4 years ago

I've been meditating about this issue and it really looks like we can do anything other than their proposed workaround if we want real time facades working on Bref 😔

Do you think it's better for me to polish that PR or do you have something different in mind?

EDIT: maybe overriding AliasLoader? https://laravel.com/docs/7.x/container#binding

hesomApptega commented 4 years ago

@yangm97 I ran into the same issue, I ended up overriding AliasLoader.php and forcing it to use the tmp directory.

tripper54 commented 3 years ago

Maybe another option would be to mount an EFS volume for the storage directory: https://aws.amazon.com/blogs/compute/using-amazon-efs-for-aws-lambda-in-your-serverless-applications/

mnapoli commented 2 years ago

@tripper54 I wouldn't recommend EFS. From my tests it is much slower, it also requires a VPC and heavy setup with base costs. It's really good as a last resort solution.

Mesuva commented 2 years ago

I've hit this issue myself trying to get Livewire file uploads working. Livewire appears to be loading up some real-time facades, and those then try to be cached in /var/task/storage/framework/cache, as per the original issue described here.

My solution so far feels a bit hacky, but does work:

$app = new App\Overrides\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
<?php

namespace App\Overrides;

class Application extends \Illuminate\Foundation\Application
{
    public function storagePath($path = '')
    {
        $isRunningInLambda = isset($_SERVER['LAMBDA_TASK_ROOT']);

        if ($isRunningInLambda && strpos($path, 'framework/cache/') === 0 ) {
            $path = str_replace('framework/cache/', '', $path);
            return '/tmp/' . $path;
        }

        return ($this->storagePath ?: $this->basePath.DIRECTORY_SEPARATOR.'storage')
            .($path != '' ? DIRECTORY_SEPARATOR.$path : '');
    }
}

I'm sure there are better ways to achieve this, but this at least feels predicable.

josegustavo commented 2 years ago

My solution was:

In bootstrap/app.php

$app->useStoragePath($_ENV['APP_STORAGE'] ?? (base_path() . '/storage'));

in .env

APP_STORAGE=/tmp/storage

in app/Providers/AppServiceProvider.php

public function boot()
{
        $storagePath = config('cache.stores.file.path', '');
        // Make sure the directory for local storage exists
        if (! is_dir($storagePath)) {
            mkdir($storagePath, 0755, true);
        }
}
mnapoli commented 1 year ago

Will be solved in v2, see https://github.com/brefphp/laravel-bridge/pull/94