Code Monkey home page Code Monkey logo

laravel-operations's Introduction

Laravel Operations

Operations allow you to schedule your jobs in a programmatic, database-driven way. By creating an operation record in your database and then calling the Operator to queue any operations that are ready to be run, you can schedule tasks for execution in the far future and keep track of every operation that has already been run.

Installation

Install the package by requiring it with Composer.

composer require dealerinspire/laravel-operations

The service provider will get registered automatically, but you will need to publish the config file.

php artisan vendor:publish --provider="DealerInspire\Operations\OperationServiceProvider"

Usage

Here is an example of a very simple Operation:

class ExampleOperation extends Operation
{
    public function run()
    {
        // Logic goes in here to be executed when the operation is run.
    }
}

If you would like to use dependency injection, you can add type-hinted parameters to your run function to be injected at runtime.

Operations are Eloquent models, and can have whatever columns on them that you want. The only requirement is that they have three additional timestamp columns, should_run_at, started_run_at, and finished_run_at.

To make sure that the Operator knows about all of your operations you should add the classname to the config\operations.php file.

'operations' => [
    \App\Operations\ExampleOperation::class,
],

To put any operations that are ready to run into your job queue, you should call the Operator's queue function.

// Using the facade.

use DealerInspire\Operations\Facades\Operator;

// ...

Operator::queue();
// Using an Operator instance.

use DealerInspire\Operations\Operator;

// ...

$operator = new Operator();
$operator->queue();

Any operation that has a should_run_at timestamp which is in the past and has a null started_run_at value (and also hasn't been deleted) will be put into your job queue.

An operation's started_run_at timestamp will be set to the current time as soon as it is placed into your queue (not when the job actually begins getting processed by a worker). Once the operation has been run by a worker the finished_run_at timestamp will be set.

You can quickly create new operations (which will be placed in your App\Operations directory) by running the make:operation.

php artisan make:operation MyNewOperation

There is also a --migration flag that you can use to create a migration with the necessary timestamps.

Documentation

More in-depth information on best practices and how to use operations effectively.

Once you're familiar with using operations, check out some these docs on more advanced features.

License

MIT © Dealer Inspire

laravel-operations's People

Contributors

austinkregel avatar jpuck avatar

Watchers

James Cloos 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.