|
| 1 | +# Backdrop |
| 2 | +Backdrop is a simple library that does one thing: allows you to run one-off |
| 3 | +tasks in the background. |
| 4 | + |
| 5 | +## How to Use |
| 6 | +```php |
| 7 | +function my_awesome_function( $id ) { |
| 8 | + // Download initial data to my site. Might take a long time! |
| 9 | + $data = wp_remote_get( 'http://example.com/' . $id ); |
| 10 | + |
| 11 | + if ( is_wp_error( $data ) ) { |
| 12 | + return $data; |
| 13 | + } |
| 14 | + |
| 15 | + update_option( 'initial_data', $data ); |
| 16 | +} |
| 17 | + |
| 18 | +add_action( 'init', function () { |
| 19 | + if ( ! get_option( 'initial_data' ) ) { |
| 20 | + $task = new \HM\Backdrop\Task( 'my_awesome_function', get_current_user_id() ); |
| 21 | + $task->schedule(); |
| 22 | + } |
| 23 | +} ); |
| 24 | +``` |
| 25 | + |
| 26 | +## API |
| 27 | +### `Task::__construct( $callback [, $...] )` |
| 28 | +Creating a new task sets up all of the internal data for your task. Pass in your |
| 29 | +callback followed by your arguments to the function, and Backdrop will call it |
| 30 | +in a background process. |
| 31 | + |
| 32 | +#### Arguments |
| 33 | +* `$callback`: Callback method you want to use. Can be any callable type |
| 34 | + (including object methods and static methods) **except for anonymous |
| 35 | + functions**. Closures cannot be serialized, so they cannot be used for |
| 36 | + Backdrop callbacks. This is an internal PHP limitation. |
| 37 | +* `$...`: Any other arguments you'd like to pass to your callback, as variable |
| 38 | + arguments. e.g. `new Task( 'a', 'b', 'c', 'd' )` maps to `a( 'b', 'c', 'd' )` |
| 39 | + |
| 40 | +#### Return Value |
| 41 | +None (constructor). |
| 42 | + |
| 43 | +### `Task::schedule()` |
| 44 | +Schedules your task to run. Typically runs after your page has been rendered, in |
| 45 | +a separate process. |
| 46 | + |
| 47 | +Backdrop de-duplicates tasks based on the arguments passed in. For example, you |
| 48 | +can do `new Task( 'myfunc', 1 )` on every request, and only one will be run. |
| 49 | +After this has been run, the next call will schedule again. |
| 50 | + |
| 51 | +To avoid this, you should pass in unique identifiers as needed. Everything that |
| 52 | +makes your task unique should be passed in and used by your function, as global |
| 53 | +state may change. |
| 54 | + |
| 55 | +#### Arguments |
| 56 | +None. |
| 57 | + |
| 58 | +#### Return Value |
| 59 | +Either `true`, or a `WP_Error` on failure. The error object will indicate the |
| 60 | +type of error; typically this is a `hm_backdrop_scheduled` if the task is |
| 61 | +already scheduled to run or is currently running. |
| 62 | + |
| 63 | +### `Task::is_scheduled()` |
| 64 | +Checks whether your task is scheduled to run. |
| 65 | + |
| 66 | +#### Arguments |
| 67 | +None. |
| 68 | + |
| 69 | +#### Return Value |
| 70 | +Boolean indicating whether your task is scheduled to run, or is already running. |
| 71 | + |
| 72 | +#### `Task::cancel()` |
| 73 | +Cancels a previously scheduled task. |
| 74 | + |
| 75 | +Note that if the task is already running, this will not cancel execution; it |
| 76 | +simply removes it from the tasks scheduled to run. |
| 77 | + |
| 78 | +#### Arguments |
| 79 | +None. |
| 80 | + |
| 81 | +#### Return Value |
| 82 | +Either `true`, or a `WP_Error` on failure. The error object will indicate the |
| 83 | +type of error; typically this is a `hm_backdrop_not_scheduled` if the task |
| 84 | +hasn't been scheduled. |
| 85 | + |
| 86 | +## Compatibility |
| 87 | +Backdrop is compatible with PHP 5.2 and upwards. |
| 88 | + |
| 89 | +### PHP 5.2 |
| 90 | +Use the `HM_Backdrop_Task` class (and `HM_Backdrop_Server`). |
| 91 | + |
| 92 | +**Important note:** If subclassing `HM_Backdrop_Server` with 5.2 compatibility, |
| 93 | +you *must* reimplement the `spawn` method, as PHP 5.2 does not include late |
| 94 | +static bindings. This is automatically handled for 5.3+. |
| 95 | + |
| 96 | +Here's a minimal implementation that you can use: |
| 97 | + |
| 98 | +``` |
| 99 | +class MyBackdrop_Server extends HM_Backdrop_Server { |
| 100 | + public static function spawn() { |
| 101 | + return self::spawn_run( __CLASS__ ); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### PHP 5.3+ |
| 107 | +Use the `HM\Backdrop\Task` class (and `HM\Backdrop\Server`). You can also import |
| 108 | +the classes with the `use` keyword; for example, `use HM\Backdrop\Task` will |
| 109 | +allow you to create tasks with `new Task`. |
| 110 | + |
| 111 | +## License |
| 112 | +Backdrop is licensed under the GPL version 2. |
| 113 | + |
| 114 | +Copyright 2014 Human Made Limited |
0 commit comments