Git Product home page Git Product logo

trackable-jobs-for-laravel's Introduction

Trackable Jobs For Laravel

Trackable jobs for laravel

Latest Version on Packagist Total Downloads MIT Licensed StyleCI

This package allows you to track your laravel jobs! Using this package, you can easily persist the output and the status of any job in your application.

Installation

To install this package, use composer:

composer require mateusjunges/laravel-trackable-jobs

You can publish the configuration file with this command:

php artisan vendor:publish --tag=trackable-jobs-config

Run php artisan migrate to migrate the table needed by this package and now you are good to go!

Usage

Tracking jobs

To start tracking your jobs, you just need to use the Junges\TrackableJobs\Traits\Trackable trait in the job you want to track. For example, let's say you want to track the status of ProcessPodcastJob, just add the Trackable trait into your job:

<?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 Junges\TrackableJobs\Traits\Trackable;

class ProcessPodcastJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable;

    public function handle()
    {
        //
    }
}

This trait provides 3 methods to your job: __construct, failed and middleware. It also adds a model public property to the job class. If you want to override any of the methods, you must copy and paste (because you can't use parent for traits) the content of each one inside your class, so this package still work as intended.

For example: if you need to change the constructor of your job, you can use the Junges\TrackableJobs\Traits\Trackable and alias the __construct with some other name, for example:

<?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 Junges\TrackableJobs\Traits\Trackable;
use App\Models\Podcast;
use Junges\TrackableJobs\Models\TrackedJob;

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

    public function __construct(Podcast $podcast)
    {
         $this->baseContruct($podcast);
         
         // Add your code here.
    }

    public function handle()
    {
        //
    }
}

It can be done with any method you want to change.

This package will store the last status of your job, which can be queued, started, failed or finished. Also, it stores the started_at and finished_at timestamps for each tracked job.

To use it, you just need to pass any model to your Job constructor:

dispatch(new ProcessPodcastJob($podcast));

Once this trait is added to your job, your job progress will be persisted to the database. You can configure the table name by publishing this package configuration file:

php artisan vendor:publish --tag=trackable-jobs-config

This command will create a new config file in config/trackable-jobs.php, with this content:

<?php

return [
    /*
     | The table where the tracked jobs will be stored.
     | By default, it's called 'tracked_jobs'.
     */
    'tables' => [
        'tracked_jobs' => 'tracked_jobs',
    ],
];

Tracking job chains

Laravel supports job chaining out of the box:

Bus::dispatchChain([
    new OptimizePodcast($podcast),
    new CompressPodcast($podcast),
    new ReleasePodcast($podcast)
])->dispatch();

It's a nice, fluent way of saying "Run this jobs sequentially, one after the previous one is complete.".

If you have a task which takes some steps to be completed, you can track the job chain used to do that and know the status for each job. If you are releasing a new podcast, for example, and it has to be optimized, compressed and released, you can track this steps by adding a steps relationship to your Podcast model:

public function steps()
{
    return $this->morphMany(Junges\TrackableJobs\Models\TrackedJob::class, 'trackable');
}

Now, you can have the status of each job that should be processed to release your podcast:

$steps = Podcast::find($id)->steps()->get();

Persist the output of a job

To persist the output of your job to the database, you only need to return something from your job. By default, if your job throws an exception, the output stored in the database will be the message of the given exception. If your job finishes successfully, you don't have to return anything, but you can store it's output by just returning something after the job is done. For example:

public function handle()
{
    //Do your stuff here
    
    return "Job finished successfully";
}

The string Job finished successfully will be stored as the output of this job.

Extending the TrackedJob model.

If, for some reason, you need to use your own custom model to the TrackedJob table, you can just create a new model and extend the existing Junges\TrackableJobs\Models\TrackedJob::class. Then, you need to bind the Junges\TrackableJobs\Contracts\TrackableJobContract to the new model, within your AppServiceProvider:

<?php

namespace App\Providers;

use App\Models\YourCustomModel;
use Illuminate\Support\ServiceProvider;
use Junges\TrackableJobs\Contracts\TrackableJobContract;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(TrackableJobContract::class, YourCustomModel::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Tests

Run composer test to test this package.

Contributing

Thank you for consider contributing for the Laravel Trackable Jobs package! The contribution guide can be found here.

Changelog

Please see the changelog for more information about the changes on this package.

Credits

License

The laravel trackable jobs is open-sourced software licensed under the terms of MIT License. Please see the license file for more information.

trackable-jobs-for-laravel's People

Contributors

mateusjunges avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.