2017-01-16 9 views
0

ファイルタイプを使用して電子メールをスプールするようにswiftmailerを設定しました。ここ symfony - kernel.terminateでコンソールコマンドを実行

swiftmailer: 
    transport: "%mailer_transport%" 
    host:  "%mailer_host%" 
    username: "%mailer_user%" 
    password: "%mailer_password%" 
    spool: 
     type: file 
     path: "%kernel.root_dir%/../var/spool" 

私は任意の電子メールも完璧にスプールを送る私のではSwiftMailerの設定

です。私はその後の電子メールを送信するために次のコマンドを実行します。

bin/console swiftmailer:spool:send --env=dev 

symfonyのdocumentation

the console command should be triggered by a cron job or scheduled task and run at a regular interval. 

によれば、私の問題は、クーロンは、私は余裕がない1分間隔の最小値を使用して構成することができるので、私はcrontabファイルを使用することはできません。私は、応答がブラウザに返された後、即座に実行するバックグラウンドプロセスを利用したいので、スプールの実行を最小限に抑えることができます。

イベントリスナークラスを作成してkernel.terminateを聞き取り、shell_execまたはexec関数を使用してコマンドを実行しようとしましたが、ここでは参照用のコードです。ここで

app.kernel.terminate.listener: 
     arguments: ["@kernel"] 
     class: AppBundle\EventListener\KernelTerminateListener 
     tags: 
      - { name: kernel.event_listener, event: kernel.terminate } 

私はここにしようとしています何私のEventListenerクラス

<?php 

namespace AppBundle\EventListener; 

use Symfony\Component\HttpKernel\Event\PostResponseEvent; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; 
use Cocur\BackgroundProcess\BackgroundProcess; 

class KernelTerminateListener 
{ 
    protected $kernel; 

    protected $console; 

    public function __construct($kernel) 
    { 
     $this->kernel = $kernel; 
     $this->console = $this->kernel->getRootDir().'/../bin/console '; 
    } 

    public function onKernelTerminate(PostResponseEvent $event) 
    { 
     $command = $this->console.'swiftmailer:spool:send --env='.$this->kernel->getEnvironment(); 
     shell_exec($command); 
    } 
} 

が、残念ながら、これは動作しません、kernel.terminateイベントでbin/console swiftmailer:spool:send --env=devを実行することである、この問題にアプローチする方法上の任意のヒントが理解されます。

ありがとうございます。

+0

「これは機能しません」とはどういう意味ですか? – COil

+0

電子メールをディスパッチしませんが、 '$ command'をechoして端末に貼り付けると動作します。私はそれが実行されても、スプールファイルは処理されませんが、通常スプールファイルが処理されると削除されることがわかります、私の場合は削除されていません。 –

+0

'shell_exec($ command);'の出力は何の問題でしょうか? – COil

答えて

1

に改名私の更新リスナークラスは、Swift Mailerののメモリスプールタイプを使用してください、それはあなたがメモリに電子メールを保存するためにスプールを使用するときは、

をしたい正確に何が、彼らカーネルが終了する直前に送信されます。 これは、要求全体が実行された場合にのみ電子メールが送信されることを意味します。未処理の例外もエラーもありません。メモリオプションを使用してswiftmailerを設定するには、次の設定を使用します。

+0

慎重に読んだら、カーネルが終了する直前に実行されます。これはバックグラウンドで実行されることを意味するものではなく、最後に実行されたと言います。 –

+0

:-)私の要求応答をクライアントに送信し、fastcgi_finish_requestた後、カーネルが= = kernel.terminateイベントを終了する前には、()私は、それは残念ながら、あなたが言いたいのかを説明複数の参照を見つけ、あなたが正しいです –

+0

と呼ばれていましたおそらくここで説明された理由のために私のために働いていないhttp://stackoverflow.com/questions/23719821/response-returned-only-after-kernel-terminate-event私は正しい方向に向いてくれてありがとう。 1 –

-1

おそらく、PHPで問題が発生しました.MAMPを使用していて、OSXにはPHPがプリインストールされています。基本的に2つのPHPバージョンがインストールされています。私はMailerSpoolListener

<?php 

namespace AppBundle\EventListener; 

use Symfony\Component\HttpKernel\Event\PostResponseEvent; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; 
use Cocur\BackgroundProcess\BackgroundProcess; 

class MailerSpoolListener 
{ 
    protected $kernel; 

    protected $php; 

    protected $console; 

    protected $env; 

    protected $command; 

    protected $muteOutput; 

    public function __construct($kernel) 
    { 
     $this->kernel = $kernel; 
     $this->php = PHP_BINDIR.'/php'; 
     $this->command = 'swiftmailer:spool:send'; 
     $this->console = $this->kernel->getRootDir().'/../bin/console'; 
     $this->env = $this->kernel->getEnvironment(); 
     $this->muteOutput = '> /dev/null 2>/dev/null &'; 
    } 

    public function onKernelTerminate(PostResponseEvent $event) 
    { 
     $command = $this->php.' '.$this->console.' '.$this->command.' --env='.$this->env.' '.$this->muteOutput; 
     $process = shell_exec($command); 
    } 
} 
関連する問題