Giter Site home page Giter Site logo

backdrop's Introduction

Backdrop

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

How to Use

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 \HM\Backdrop\Task( 'my_awesome_function', get_current_user_id() );
		$task->schedule();
	}
} );

API

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.

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).

Task::schedule()

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

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.

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.

Arguments

None.

Return Value

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

Task::is_scheduled()

Checks whether your task is scheduled to run.

Arguments

None.

Return Value

Boolean indicating whether your task is scheduled to run, or is already running.

Task::cancel()

Cancels a previously scheduled task.

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

Arguments

None.

Return Value

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

Compatibility

Backdrop is compatible with PHP 5.2 and upwards.

PHP 5.2

Use the HM_Backdrop_Task class (and HM_Backdrop_Server).

Important note: If subclassing HM_Backdrop_Server with 5.2 compatibility, you must reimplement the spawn method, as PHP 5.2 does not include late static bindings. This is automatically handled for 5.3+.

Here's a minimal implementation that you can use:

class MyBackdrop_Server extends HM_Backdrop_Server {
	public static function spawn() {
		return self::spawn_run( __CLASS__ );
	}
}

PHP 5.3+

Use the HM\Backdrop\Task class (and HM\Backdrop\Server). You can also import the classes with the use keyword; for example, use HM\Backdrop\Task will allow you to create tasks with new Task.

License

Backdrop is licensed under the GPL version 2.

Copyright 2014 Human Made Limited

backdrop's People

Contributors

joehoyle avatar johnbillion avatar mattheu avatar rmccue avatar scottbasgaard avatar willmot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backdrop's Issues

Default wp_options.option_name size collides with SHA1 hash

By default wp_options.option_name can hold 64 characters. The name for transient timeouts is _transient_timeout_hm_backdrop- plus the 40 character SHA1 hash, which is a total of 71 characters.

Calling get_transient will always return false because the last 5 characters are missing.

Why not use MD5? It will result in a 63 character transient timeout name. Colliding keys are very unlikely anyway and I don't think there are serious security implications.

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.