class Worker extends Zookeeper {
const CONTAINER = '/cluster';
protected $acl = array(
array(
'perms' => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone' ) );
private $isLeader = false;
private $znode;
public function __construct( $host = '', $watcher_cb = null, $recv_timeout = 10000 ) {
parent::__construct( $host, $watcher_cb, $recv_timeout );
}
public function register() {
if( ! $this->exists( self::CONTAINER ) ) {
$this->create( self::CONTAINER, null, $this->acl );
}
$this->znode = $this->create( self::CONTAINER . '/w-',
null,
$this->acl,
Zookeeper::EPHEMERAL | Zookeeper::SEQUENCE );
$this->znode = str_replace( self::CONTAINER .'/', '', $this->znode );
Example 3
Multiple workers with automatic leader election
# console 1
$ php example_worker.php
I'm registred as: w-0000000001
Nobody here, I'm the leader
Leading
# console 2
$ php example_worker.php
I'm registred as: w-0000000002
I'm watching w-0000000001
Working
# console 3
$ php example_worker.php
I'm registred as: w-0000000003
I'm watching w-0000000002
Working