1

Goal: is to dispatch job: right after user is logged in into the system.

So I have placed dispatch code in: app/Http/Middleware/RedirectIfAuthenticated.php

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param  \Closure  $next
     * @param  string|null  ...$guards
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                MyJob::dispatch(Auth::user()); // HERE I DISPATCH MY JOB
                return redirect(theme()->getPageUrl(RouteServiceProvider::HOME));
            }
        }
        return $next($request);
    }
}

This is my simple job example:-

<?php

namespace App\Jobs;

use App\Models\User;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

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

    public User $user;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
             Log::info('JOB STARTED : ' .now());
             sleep(5);
             Log::info('JOB ENDED : ' .now());
        } catch (Exception $exception) {
            Log::info('---------------------------------------------------------------------------------------');
            Log::info('****************************************');
            Log::info('CLASS : ' . __CLASS__);
            Log::info('METHOD : ' . __METHOD__);
            Log::info('FUNCTION : ' . __FUNCTION__);
            Log::info('DIRECTORY : ' . __DIR__);
            Log::info('****************************************');
            Log::info('EXCEPTION', [
                'status' => 'error',
                'message' => $exception->getMessage(),
                'code' => $exception->getCode(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTrace(),
            ]);
        }
    }

}

It doesn't work until I clear the browser cache or run in incognito mode of the browser.Also it's running only once, even if I logout and login again this job won't exceute or dispatch. I'm running my project on aws ec2 machine and this is my supervisor config:-

[program:laravel_worker]
command=php /var/www/development/artisan queue:work
process_name=%(program_name)s_%(process_num)02d
autostart=false
autorestart=true
stopsgroup=true
user=root
numprocs=3
stderr_logfile=/var/www/development/laraqueue.err.log
stdout_logfile=/var/www/development/laraqueue.out.log
stopwaitsecs=3600

Already tried all the following commands:-

php artisan queue:clear
php artisan config:cache
php artisan optimize:cache

I don't see the problem with my code, maybe something to do with user=root in supervisor config, because my project is under Ubuntu group and Ubuntu user? I don't I'm super confused where to go.

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43

0 Answers0