Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from ConnectHolland/mongo-scheduler
Browse files Browse the repository at this point in the history
Mongo scheduler
  • Loading branch information
RonRademaker committed Sep 29, 2015
2 parents 057d980 + 8880127 commit 0be8fc1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
nbproject/
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ php:
- 5.5
- 5.6

services:
- mongodb

before_script:
- echo 'extension = mongo.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- composer self-update
- composer install

Expand Down
80 changes: 80 additions & 0 deletions src/Scheduler/MongoScheduler.php
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();
}
}
72 changes: 72 additions & 0 deletions tests/Scheduler/MongoSchedulerTest.php
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 added tests/schedulerpath/.gitkeep
Empty file.

0 comments on commit 0be8fc1

Please sign in to comment.