Giter Site home page Giter Site logo

laravel-delay's Introduction

Laravel Delay

Package which helps you to delay (sleep) your code for some time.

Note before continuing

As of Laravel version 10.10.0 you may not need this package anymore in favour of in-built helper - https://laravel.com/docs/10.x/helpers#sleep

Installation

Requirements

  • Laravel 8.12+, 9.0+, 10.0+
  • PHP 8.0

Installation

Require the package via Composer:

composer require newman/laravel-delay

๐Ÿ“– Usage

Using as Facade

use Newman\LaravelDelay\Facades\Delay;

// ...

Delay::for(3);

Using as Trait

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Newman\LaravelDelay\Traits\Delayable;

class ImportTask extends Command {
    use Delayable;
    
    // ...
}

then in code you can call the delay the code execution like this:

$this->delay()->for(5);

or

$this->delay(5);

They both will delay execution for 5 seconds.

You can include trait in any class you'd like to use it, including controllers.

Using as service container

use Newman\LaravelDelay\Contracts\DelayContract;

// ...

app()->make(DelayContract::class)->for(5);

Usage samples

Let's assume we're using Trait.

Delay execution for 10 seconds:

$this->delay(10);
$this->delay()->for(10);
$this->delay()->forSeconds(10);

Note that in case you want to delay for fractions of a second, you should use forMs function.

Delay execution for 1500 miliseconds (1.5 second):

$this->delay()->forMs(1500);
$this->delay()->forMiliseconds(1500);

Delay execution for 5000 microseconds (0.005 second):

$this->delay()->forMicroseconds(5000);

Delay execution till given Carbon datetime:

$this->delay()->till(Carbon::now()->addMinutes(5)->addSeconds(15));

Delay execution for 10 seconds only on given environment/-s:

$this->delay()->for(10)->environments(['production']); // delays only on production
$this->delay()->for(10)->environments(['production', 'staging']); // delays on production and staging only

Delay execution for 10 seconds except given environment/-s:

$this->delay()->for(10)->except(['prodction']); // delays on all environments, except production
$this->delay()->for(10)->except(['prodction', 'staging']); // delays on all environments, except production and staging

Delay execution for 10 seconds only when callback returns false:

$this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 2); // code will not delay in this case, because callback returns true
$this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 3); // code will delay in this case, because callback returns false

and we can even pass multiple callbacks.

$this->delay()->for(10)->exceptWhen(fn () => false)->exceptWhen(fn () => false); // code will delay
$this->delay()->for(10)->exceptWhen(fn () => true)->exceptWhen(fn () => false); // code will not delay, because all callbacks doesn't return false

At last we can chain multiple conditions:

It will delay for 10 seconds only on production & staging environments and only when it's not 10 AM.

$this->delay()
    ->for(10)
    ->environments(['production', 'staging'])
    ->exceptWhen(fn () => Carbon::now()->hour == 10);

๐Ÿค Contributing

We'll appreciate your collaboration to this package.

When making pull requests, make sure:

  • All tests are passing: composer test
  • Test coverage is not reduced: composer test-coverage
  • There are no PHPStan errors: composer phpstan
  • Coding standard is followed: composer lint or composer fix-style to automatically fix it.

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.