This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ConnectHolland/mongo-scheduler
Mongo scheduler
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
nbproject/ |
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,80 @@ | ||
<?php | ||
namespace ConnectHolland\Tactician\SchedulerPlugin\Scheduler; | ||
|
||
use ConnectHolland\Tactician\SchedulerPlugin\Command\ScheduledCommandInterface; | ||
use MongoCollection; | ||
use MongoDate; | ||
use MongoDB; | ||
use MongoId; | ||
|
||
/** | ||
* Scheduler that uses a mongo database to store the commands | ||
* | ||
* @author Ron Rademaker | ||
*/ | ||
class MongoScheduler implements SchedulerInterface | ||
{ | ||
/** | ||
* Collection to store in | ||
* | ||
* @var MongoCollection | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* Creates a new MongoScheduler | ||
* | ||
* @access public | ||
* @since 1.1 | ||
* @param MongoDB $mongoDB | ||
* @param string $collection | ||
* @return void | ||
*/ | ||
public function __construct(MongoCollection $collection) | ||
{ | ||
$this->collection = $collection; | ||
} | ||
|
||
/** | ||
* Gets the commands that should be executed and removes them from the database | ||
* | ||
* @access public | ||
* @since 1.1 | ||
* @return array | ||
*/ | ||
public function getCommands() | ||
{ | ||
$query = [ | ||
'timestamp' => ['$lt' => new MongoDate()] | ||
]; | ||
|
||
$storedCommands = $this->collection->find($query); | ||
$commands = []; | ||
foreach ($storedCommands as $storedCommand) { | ||
$commands[] = unserialize($storedCommand['command']); | ||
$this->collection->remove(['_id' => $storedCommand['_id']]); | ||
} | ||
|
||
return $commands; | ||
} | ||
|
||
/** | ||
* Schedules $command to be executed at its set time | ||
* | ||
* @param ScheduledCommandInterface $command | ||
* @param type $id | ||
* @return $id | ||
*/ | ||
public function schedule(ScheduledCommandInterface $command, $id = null) | ||
{ | ||
$mongoId = isset($id) ? new MongoId($id) : new MongoId(); | ||
$data = [ | ||
'_id' => $mongoId, | ||
'command' => serialize($command), | ||
'timestamp' => new MongoDate($command->getTimestamp()) | ||
]; | ||
$this->collection->save($data); | ||
|
||
return $mongoId->__toString(); | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
namespace ConnectHolland\Tactician\SchedulerPlugin\Scheduler\Tests; | ||
|
||
use ConnectHolland\Tactician\SchedulerPlugin\Scheduler\MongoScheduler; | ||
use ConnectHolland\Tactician\SchedulerPlugin\Tests\Fixtures\Command\ScheduledCommand; | ||
use MongoClient; | ||
use MongoId; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Unit test to test scheduling commands using a mongo database | ||
* | ||
* @author Ron Rademaker | ||
*/ | ||
class MongoSchedulerTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Drop any leftover test commands | ||
*/ | ||
public function tearDown() | ||
{ | ||
$con = new MongoClient('mongodb://localhost'); | ||
$db = $con->selectDB('ConnectHollandTacticianSchedulerTest'); | ||
$db->dropCollection('MongoScheduler'); | ||
} | ||
|
||
/** | ||
* testScheduleCommand | ||
*/ | ||
public function testScheduleCommand() | ||
{ | ||
$con = new MongoClient('mongodb://localhost'); | ||
$db = $con->selectDB('ConnectHollandTacticianSchedulerTest'); | ||
$collection = $db->selectCollection('MongoScheduler'); | ||
$scheduler = new MongoScheduler($collection); | ||
|
||
$command = new ScheduledCommand(); | ||
$command->setTimestamp(time() + 1); | ||
$identifier = $scheduler->schedule($command); | ||
|
||
$stored = $collection->findOne(['_id' => new MongoId($identifier)]); | ||
|
||
$this->assertEquals($command, unserialize($stored['command'])); | ||
$collection->remove(['_id' => new MongoId($identifier)]); | ||
} | ||
|
||
/** | ||
* testGetCommands | ||
*/ | ||
public function testGetCommands() | ||
{ | ||
$con = new MongoClient('mongodb://localhost'); | ||
$db = $con->selectDB('ConnectHollandTacticianSchedulerTest'); | ||
$collection = $db->selectCollection('MongoScheduler'); | ||
$scheduler = new MongoScheduler($collection); | ||
|
||
$command = new ScheduledCommand(); | ||
$command->setTimestamp(time() + 1); | ||
$identifier = $scheduler->schedule($command); | ||
$nothing = $scheduler->getCommands(); | ||
$this->assertEquals(0, count($nothing)); | ||
sleep(1); | ||
|
||
$todo = $scheduler->getCommands(); | ||
$this->assertEquals(1, count($todo)); | ||
$this->assertEquals($command, $todo[0]); | ||
|
||
$stored = $collection->findOne(['_id' => new MongoId($identifier)]); | ||
|
||
$this->assertEmpty($stored); | ||
} | ||
} |
Empty file.