mateusjunges / trackable-jobs-for-laravel

This package allows you to easily track your laravel jobs!
https://mateusjunges.com
MIT License
280 stars 16 forks source link

Unable to pass variables to job? #34

Closed scottgrobinson closed 9 months ago

scottgrobinson commented 2 years ago

Hello!

I seem to be struggling to pass variables to my job... Also trying out your PR which is making things better but still having issues (https://github.com/mateusjunges/trackable-jobs-for-laravel/pull/33)

I call my job with: GenerateAudioLogsPlaylist::dispatch($startTimestamp, $endTimestamp)

And my job looks similar to...

<?php

namespace App\Jobs;

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\Storage;
use Junges\TrackableJobs\Concerns\Trackable;

class GenerateAudioLogsPlaylist implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable {
        __construct as __baseConstruct;
    }

    protected $startTimestamp;
    protected $endTimestamp;

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

I get the following which makes sense because your construct requires two variables trackable/shouldBeTracked), but how do I go about passing in variables?

Too few arguments to function App\Jobs\GenerateAudioLogsPlaylist::__construct(), 1 passed in /var/www/app/vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php on line 17 and exactly 2 expected
mateusjunges commented 2 years ago

Hi @scottgrobinson I'm returning from my vacation trip today, I'll look into this tomorrow. Thanks for reporting.

scottgrobinson commented 2 years ago

@mateusjunges Thanks for letting me know! I worked around this with a status table for my single job (as it's just the one job I needed this for). I won't specifically need this fix myself but appreciate the update :)

DimaVIII commented 2 years ago

@scottgrobinson In general you don't need to track the runtime of the job as this is already done with the timestamps of the records. Calculate the runtime with the started_at and finished_at timestamp or with created_at and finished_at if the job got retried. Also Laravel Horizon Dashboard gives you more details on runtime, you may be able to load the runtime direct from the Redis Database.

mateusjunges commented 2 years ago

Hi @scottgrobinson sorry for my late response.

Actually, with #33 the __constructor method expects null or a Model instance as the first parameter, and a bool as the second parameter, indicating whether the job should be tracked or not, so you can't pass the two timestamps to the __baseConstruct method.

Also, what @DimaVIII said is correct, you can calculate the duration of the job using the started_at and finished_at fields.

I seem to be struggling to pass variables to my job... Also trying out your PR which is making things better but still having issues (#33)

About this, the PR is not production ready yet, so please don't use it on production before it is released. Since I was on vacations I was not working on it, but I'll get back to it during this week.

Please let me know if there is anything more that I can help you with.