-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Batch #107
Open
henrikbjorn
wants to merge
3
commits into
bernardphp:master
Choose a base branch
from
henrikbjorn:batch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[RFC] Batch #107
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Bernard; | ||
|
||
use Bernard\Batch\Storage; | ||
use Bernard\Batch\Status; | ||
|
||
final class Batch implements Message | ||
{ | ||
private $name; | ||
private $description; | ||
private $status; | ||
private $envelopes = array(); | ||
|
||
public function __construct($name = null, $description = '', Status $status = null) | ||
{ | ||
$this->name = $name ?: uniqid('bernard_batch', true); | ||
$this->description = $description; | ||
$this->status = $status ?: new Status(0, 0, 0); | ||
} | ||
|
||
public function assign(Message $message) | ||
{ | ||
$this->envelopes[] = new Envelope($message, array('batch' => $this->name)); | ||
|
||
$this->status = new Status($this->status->total + 1, $this->status->failed, $this->status->successful); | ||
} | ||
|
||
/** | ||
* Internal method used by the producer to produce the envelopes | ||
* through its mnamedlewares. | ||
* | ||
* @return array | ||
*/ | ||
public function flush() | ||
{ | ||
$envelopes = $this->envelopes; | ||
$this->envelopes = array(); | ||
|
||
return $envelopes; | ||
} | ||
|
||
public function isRunning() | ||
{ | ||
return $this->status->isRunning(); | ||
} | ||
|
||
public function isComplete() | ||
{ | ||
return $this->status->isComplete(); | ||
} | ||
|
||
public function getStatus() | ||
{ | ||
return $this->status; | ||
} | ||
|
||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Bernard\Batch; | ||
|
||
use Bernard\Batch; | ||
|
||
abstract class AbstractStorage implements Storage | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function reload(Batch $batch) | ||
{ | ||
return $this->find($batch->getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Bernard\Batch; | ||
|
||
use Bernard\Batch; | ||
use Redis; | ||
|
||
/** | ||
* Storage specific to php redis extension. | ||
* | ||
* @package Bernard | ||
*/ | ||
class RedisStorage extends AbstractStorage | ||
{ | ||
protected $redis; | ||
protected $ttl; | ||
|
||
/** | ||
* @param Redis $redis | ||
* @param integer $ttl | ||
*/ | ||
public function __construct(Redis $redis, $ttl = null) | ||
{ | ||
$this->redis = $redis; | ||
$this->ttl = $ttl ?: 3600 * 36; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function find($name) | ||
{ | ||
$result = $this->redis->hmget($this->resolveKey($name), array('total', 'failed', 'successful', 'description')); | ||
|
||
return new Batch($name, $result['description'], new Status($result['total'], $result['failed'], $result['successful'])); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function register($name) | ||
{ | ||
$redis = $this->redis->multi(); | ||
|
||
// insert the batch name into the set if dosent exists all ready. | ||
// we use sorted sets to cheat into not really expiring them. | ||
// we update this on last insert into the batch, to make sure we | ||
// dont add message and expire the batch instantanious. | ||
$redis->zadd('batches', $this->ttl + time(), $name); | ||
$redis->incr($this->resolveKey($name, 'total')); | ||
|
||
// create sets with expire at | ||
$redis->expire($this->resolveKey($name, 'total'), $this->ttl); | ||
$redis->expire($this->resolveKey($name, 'failed'), $this->ttl); | ||
$redis->expire($this->resolveKey($name, 'successful'), $this->ttl); | ||
|
||
// fire | ||
$redis->exec(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function increment($name, $type) | ||
{ | ||
if (!in_array($type, array('failed', 'successful'))) { | ||
// should be more precise | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
// creates a new key if dosent exists, creates a new field with a | ||
// 0 as default if not exists. | ||
$this->redis->incr($this->resolveKey($name, $type)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function all() | ||
{ | ||
$batches = $this->redis->zrangebyscore('batches', time(), '+inf'); | ||
|
||
return array_map($batches, array($this, 'find')); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function resolveKey($name, $type = null) | ||
{ | ||
return rtrim('batch:' . $name . ':' . $type, ':'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Bernard\Batch; | ||
|
||
/** | ||
* ValueObject that holds different information about the current | ||
* status of a Batch | ||
* | ||
* @package Bernard | ||
*/ | ||
final class Status | ||
{ | ||
private $total; | ||
private $failed; | ||
private $successful; | ||
|
||
public function __construct($total, $failed, $successful) | ||
{ | ||
$this->total = $total; | ||
$this->failed = $failed; | ||
$this->successful = $successful; | ||
} | ||
|
||
public function isComplete() | ||
{ | ||
return $this->total == $this->failed + $this->successful; | ||
} | ||
|
||
public function isRunning() | ||
{ | ||
return false == $this->isComplete(); | ||
} | ||
|
||
public function __get($property) | ||
{ | ||
return $this->$property; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Bernard\Batch; | ||
|
||
use Bernard\Batch; | ||
|
||
/** | ||
* Handles storage support for Batch. Implement this interface | ||
* to use another storage backend. | ||
* | ||
* @package Bernard | ||
*/ | ||
interface Storage | ||
{ | ||
/** | ||
* @param integer $id | ||
* @return Batch | ||
*/ | ||
public function find($id); | ||
|
||
/** | ||
* Returns a new batch based on the given instance indentifier. | ||
* | ||
* @param Batch $batch | ||
* @return Batch | ||
*/ | ||
public function reload(Batch $batch); | ||
|
||
/** | ||
* @return Batch[] | ||
*/ | ||
public function all(); | ||
|
||
/** | ||
* Register a batch with the storage, this may | ||
* be called multiple times. As it is called by the | ||
* middleware everytime a batch have been added | ||
* | ||
* @param string $id | ||
*/ | ||
public function register($id); | ||
|
||
/** | ||
* Increments a counter for a specific batch. | ||
* | ||
* @param string $id | ||
* @param string $type | ||
*/ | ||
public function increment($id, $type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Bernard\EventListener; | ||
|
||
use Bernard\Event\EnvelopeEvent; | ||
use Bernard\Event\RejectEnvelopeEvent; | ||
use Bernard\Batch\Storage; | ||
use Bernard\Envelope; | ||
|
||
class BatchSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface | ||
{ | ||
protected $storage; | ||
|
||
public function __construct(Storage $storage) | ||
{ | ||
$this->storage = $storage; | ||
} | ||
|
||
public function onProduce(EnvelopeEvent $event) | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
|
||
if ($batch = $envelope->getStamp('batch')) { | ||
$this->storage->register($envelope->getStamp('batch')); | ||
} | ||
} | ||
|
||
public function onAcknowledgeReject(EnvelopeEvent $event) | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
|
||
if (false == $batch = $envelope->getStamp('batch')) { | ||
return; | ||
} | ||
|
||
$type = $event instanceof RejectEnvelopeEvent ? 'failed' : 'successful'; | ||
$this->storage->increment($batch, $type); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
'bernard.produce' => 'onProduce', | ||
'bernard.acknowledge' => 'onAcknowledgeReject', | ||
'bernard.reject' => 'onAcknowledgeReject', | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 3600 * 36?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought it as a good default value :)