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

Improvement Readme - Setup output content #12

Closed tiagoamemiya closed 3 years ago

tiagoamemiya commented 3 years ago

Hi Mateus, nice job with this package.

Bellow a sugestion to improve the documentation about how to setup output content:

Set Output Content

Easy to setup a output content on workflow job, bellow a example on handle method of job class:

public function handle()
{
    try {
        // Workflow Job
        $this->trackedJob->markAsFinished('Finished with success!');
    } catch (Throwable $e) {
        $this->trackedJob->markAsFailed('Oh No, an error occurred!');
    }
}
mateusjunges commented 3 years ago

Hi @tiagoamemiya,

The output is saved automatically if the job fails. For example, if an exception occurs, the job middleware provided by this package tries to get the message from the exception, and, if there is a message, it will be persisted to the database.

https://github.com/mateusjunges/trackable-jobs-for-laravel/blob/7d7636e74298577e695d67bf756bc78ab0060eff/src/Jobs/Middleware/TrackedJobMiddleware.php#L11-L19

Basically, what this middleware does is wrap the job with a try catch statement, and if something goes wrong, the job is marked as failed and the exception message saved to the database. If the job finishes without any exceptions, then it will be marked as finished https://github.com/mateusjunges/trackable-jobs-for-laravel/blob/7d7636e74298577e695d67bf756bc78ab0060eff/src/Jobs/Middleware/TrackedJobMiddleware.php#L16

If you manually mark the job as failed, for some reason, without throwing any exceptions, the job is still considered as a successfull job, since laravel queue system works as a daemon that picks up jobs, run them and moves on.

If you want to store the output of any successfull job, you just need to return something from that job. For example, if my job ends with

return "Job finished successfully";

my database record will have Job finished successfully as the job output.

I'll add it to the readme to leave no doubt.

tiagoamemiya commented 3 years ago

Hi Mateus, thanks for the feedback! Nice job with this plugin, i will use it in some projects here. :rocket: