0

アプリケーションがローカルで完全に機能しているにもかかわらず、Laravel Vapor を介してデプロイされたアプリケーションで、フロントエンド (Laravel Echo + Pusher) でイベントが「トリガー」されない理由を見つけるのに苦労しました。

プッシャーのデバッグ コンソールは、イベントが実際に Laravel アプリケーションによってプッシャーにディスパッチされていることを実際に示していました。

何が問題なのかを見つけるのに半日ほど費やしたので (幸運なことに、ステージングに何かを投稿した後、ステージングするのではなく、ローカル環境でメッセージがリアルタイムで表示されるのを見ました)、あと 10 日を費やします。ここに投稿を書くのに数分かかるので、(うまくいけば)多くの時間を無駄にする必要がない人もいます.

4

2 に答える 2

0

問題は実際には次のとおりでした。

  • Vapor は最初にアプリケーションをローカルの.vaporディレクトリにビルドします。
  • このビルド プロセス中にロードされた からの PUSHER キーは.env、ローカル.env(!) にあるものです。つまりMIX_PUSHER_APP_KEYMIX_PUSHER_APP_CLUSTER環境変数.env.{environment}( vapor env:pull {environment}.

私はこれを迅速な(そして汚い)方法で解決しました:

  • さらにいくつかの環境変数を追加します。
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_LOCAL_APP_KEY=
PUSHER_LOCAL_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_STAGING_APP_KEY=
PUSHER_STAGING_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_PRODUCTION_APP_KEY=
PUSHER_PRODUCTION_APP_CLUSTER=eu

MIX_PUSHER_APP_KEY="${PUSHER_LOCAL_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_LOCAL_APP_CLUSTER}"
  • 新しいコマンド (yarn run productionの前) を Vapor ビルド プロセスに追加しますphp artisan pusher:credentials {environment}。したがって、ステージングには を使用しますphp artisan pusher:credentials staging

コマンドは次のようになります。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Str;

final class SwapPusherCredentials extends Command
{
    use ConfirmableTrait;

    private array $keyPatterns = [
        'PUSHER_%sAPP_KEY',
        'PUSHER_%sAPP_CLUSTER',
    ];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pusher:credentials {environment=production}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sets the pusher credentials based on the current environment';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws \Exception
     */
    public function handle()
    {
        // Basically we're fixing a bug (?) in Vapor. Since we're building the front end locally,
        // we need to swap the pusher keys before building. If we don't do this, the front end
        // will be built with local pusher keys. When posting messages not local it's broken
        $environment = Str::upper($this->argument('environment'));

        $this->updatePusherEnvironmentVariables($environment);
    }

    /**
     * @param string $environment
     * @throws \Exception
     */
    private function updatePusherEnvironmentVariables(string $environment)
    {
        foreach ($this->keyPatterns as $pattern) {
            // 'PUSHER_LOCAL_APP_KEY' || 'PUSHER_STAGING_APP_KEY' etc.
            $variableToSet = sprintf($pattern, $environment . '_');

            // 'PUSHER_APP_KEY'
            $targetVariableName = sprintf($pattern, '');

            if (!env($targetVariableName, false)) {
                throw new \Exception('Missing environment value for ' . $targetVariableName);
            }

            $this->setVariable($targetVariableName, $variableToSet);
        }
    }

    private function setVariable(string $targetVariableName, string $variableToSet)
    {
        file_put_contents($this->laravel->environmentFilePath(), preg_replace(
            $this->replacementPattern($targetVariableName),
            $replacement = '${' . $variableToSet . '}',
            file_get_contents($this->laravel->environmentFilePath())
        ));

        $this->info("Successfully set MIX_{$targetVariableName} to {$replacement}!");
    }

    private function replacementPattern(string $targetVariableName)
    {
        // Don't remove notsurebutfixes, when removed it doesn't match the first entry
        // So it will match all keys except PUSHER_LOCAL_* for some reason.
        $escaped = '\=notsurebutfixes|\$\{' . $this->insertEnvironmentIntoKey('LOCAL',
                $targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('STAGING',
                $targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('PRODUCTION',
                $targetVariableName) . '\}';

        return "/^MIX_$targetVariableName{$escaped}/m";
    }

    // Insert the environment after PUSHER_ in any PUSHER_* like variable passed in.
    // So basically PUSHER_APP_KEY => PUSHER_LOCAL_APP_KEY
    private function insertEnvironmentIntoKey($environment, $key)
    {
        $parts = explode('_', $key, 2);

        return $parts[0] . '_' . $environment . '_' . $parts[1];
    }
}

それでおしまい。これ.envで、ディレクトリ内のファイルが.vapor展開中に更新され、PUSHER_STAGING_APP_KEYおよびPUSHER_STAGING_APP_CLUSTER!

于 2020-04-06T15:04:27.970 に答える