2017 © Pedro Peláez
 

library backdrop

Backdrop

image

dimadin/backdrop

Backdrop

  • Tuesday, February 13, 2018
  • by dimadin
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Backdrop

Build Status Latest Stable Version, (*1)

Backdrop is a simple library that does one thing: allows you to run one-off tasks in the background., (*2)

How to Use

Backdrop is available as Composer package that you can use in your project., (*3)

composer require dimadin/backdrop

Alternately, you can download and include all files from /inc directory in your project. In either case, you then need to register it with Main::init() not after admin_init hook., (*4)

In this example, we create task if option doesn't exist, pass parameter to it, and then schedule it, while also hooking initialization of Backdrop:, (*5)

function my_awesome_function( $id ) {
    // Download initial data to my site. Might take a long time!
    $data = wp_remote_get( 'http://example.com/' . $id );

    if ( is_wp_error( $data ) ) {
        return $data;
    }

    update_option( 'initial_data', $data );
}

add_action( 'init', function () {
    if ( ! get_option( 'initial_data' ) ) {
        $task = new \dimadin\WP\Library\Backdrop\Task( 'my_awesome_function', get_current_user_id() );
        $task->schedule();
    }
} );

add_action( 'admin_init', [ '\dimadin\WP\Library\Backdrop\Main', 'init' ] );

API

Full code reference is available at http://api.milandinic.com/backdrop/., (*6)

Task::__construct( $callback [, $...] )

Creating a new task sets up all of the internal data for your task. Pass in your callback followed by your arguments to the function, and Backdrop will call it in a background process., (*7)

Arguments

  • $callback: Callback method you want to use. Can be any callable type (including object methods and static methods) except for anonymous functions. Closures cannot be serialized, so they cannot be used for Backdrop callbacks. This is an internal PHP limitation.
  • $...: Any other arguments you'd like to pass to your callback, as variable arguments. e.g. new Task( 'a', 'b', 'c', 'd' ) maps to a( 'b', 'c', 'd' )

Return Value

None (constructor)., (*8)

Task::schedule()

Schedules your task to run. Typically runs after your page has been rendered, in a separate process., (*9)

Backdrop de-duplicates tasks based on the arguments passed in. For example, you can do new Task( 'myfunc', 1 ) on every request, and only one will be run. After this has been run, the next call will schedule again., (*10)

To avoid this, you should pass in unique identifiers as needed. Everything that makes your task unique should be passed in and used by your function, as global state may change., (*11)

Arguments

None., (*12)

Return Value

Either true, or a WP_Error on failure. The error object will indicate the type of error; typically this is a md_backdrop_scheduled if the task is already scheduled to run or is currently running., (*13)

Task::is_scheduled()

Checks whether your task is scheduled to run., (*14)

Arguments

None., (*15)

Return Value

Boolean indicating whether your task is scheduled to run, or is already running., (*16)

Task::cancel()

Cancels a previously scheduled task., (*17)

Note that if the task is already running, this will not cancel execution; it simply removes it from the tasks scheduled to run., (*18)

Arguments

None., (*19)

Return Value

Either true, or a WP_Error on failure. The error object will indicate the type of error; typically this is a md_backdrop_not_scheduled if the task hasn't been scheduled., (*20)

Main::init()

Register Backdrop., (*21)

Arguments

None., (*22)

Return Value

None., (*23)

Compatibility

Backdrop is compatible with PHP 5.4 and upwards., (*24)

License

Backdrop is licensed under the GPL version 2., (*25)

Copyright 2014 Human Made Limited, 2015-2018 Milan Dinić, (*26)

The Versions

13/02 2018

dev-master

9999999-dev https://github.com/dimadin/Backdrop

Backdrop

  Sources   Download

GPL GPL-2.0-or-later

The Requires