Homestead上的多个队列失败,事件不会被触发

时间:2019-09-08 14:04:37

标签: php laravel homestead

我目前有一个实时应用程序,我也在进行巨大的更改,因此我正在使用Homestead在本地工作。我以前使用了一个队列,但是进行了更改,并且我不得不运行多个队列。以前的默认队列用于在数据库表中创建行,而最近的队列之一则用于发送电子邮件。然后我需要另一个我也需要在另一个表中创建行...问题有时是可行的,而其他时候却失败了,有时它只会在失败之前创建一行。最令人讨厌的是,它应该为数据库中创建的每一行触发一个事件,但根本不会发生。我以前做得很有效,即使默认队列仍然可以正常工作,它也从未使我失败。

这是控制器:

<?php

namespace App\Http\Controllers;

use Redirect;
use App\Short;
use App\Wallet;
use \SplFixedArray;
use App\Jobs\ShortQueuer;
use Illuminate\Http\Request;
use App\Http\Requests\SlotRequest;

class ShortController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('short_term_goals');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SlotRequest $request)
    {
        $quota = 2;
        $slotquota = request('slotamount') + $quota;

        if ( auth()->user()->wallet->balance < $slotquota ) {

            return Redirect::back()->with('low_balance', 'You do not have a sufficient wallet balance to reserve these SLOTS. Please Load Up Your Wallet');

        } else {

            // Getting SLOTS as objects of an array
            $slotquantity = new SplFixedArray(request('slotamount'));
            $slotquantity = $slotquantity->toArray();
            $user = auth()->user();

            ShortQueuer::dispatch($slotquantity, $user)->onQueue('shorts');

        }

            //Sorting Wallet Balance
            $wallet = Wallet::where('user_id', auth()->user()->id)->first();
            $wallet->balance = $wallet->balance - $slotquota;
            $wallet->save();



            //Returning View With Message
            return Redirect::back()->with('reserved', 'Your Short Term Goals are Currently being met .');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Short  $short
     * @return \Illuminate\Http\Response
     */
    public function show(Short $short)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Short  $short
     * @return \Illuminate\Http\Response
     */
    public function edit(Short $short)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Short  $short
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Short $short)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Short  $short
     * @return \Illuminate\Http\Response
     */
    public function destroy(Short $short)
    {
        //
    }
}

这是工作

<?php

namespace App\Jobs;


use App\Short;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ShortQueuer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $slotquantity;
    protected $user;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(array $slotquantity, $user)
    {
       $this->slotquantity = $slotquantity;
       $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

       // Inserting Rows in SLOTS Table
       foreach ($this->slotquantity as $short) {

        $short = new Short();

        $short->user_id = $this->user->id;

        $short->save();

         //Slot Counting Event
         event(new ShortCounter);


        }

    }

    public $tries = 1;

    public $timeout = 86400;

    public $retryAfter = 87000;
}

这是活动

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ShortCounter
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

这里是听众

<?php

namespace App\Listeners;

use App\Short;
use App\Goal;
use App\Events\ShortCounter;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class GoalCreator
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ShortCounter  $event
     * @return void
     */
    public function handle(ShortCounter $event)
    {
        $shortcount = Short::all()->count();

        if($shortcount == 800) {

            $goalshort = Short::latest()->first();

            $goal = new Goal();

            $goal->gid = unique_random('goals', 'sgid', 8);

            $goal->grc = unique_random('goals', 'sgrc', 12);

            $goal->status = 0;

            $goal->amount = 200;

            $goal->user_id = $goalshort->user_id;

            $goal->save();

            Short::truncate();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我将其整理出来.....我遍历了错误日志并一个接一个地解决了错误。首先,我忘记在作业中包括事件类,然后数据库中的列名错误,然后重新启动服务器。