-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexandre Gomes Gaigalas
committed
Sep 28, 2010
1 parent
548c3e2
commit a9a6558
Showing
9 changed files
with
314 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,31 @@ | ||
h2. About | ||
|
||
Respect\Daemon is PHP component to create and manage deamons using OS specific | ||
tools. | ||
|
||
h3. Sample Usage (not working yet) | ||
|
||
<pre> | ||
use Respect\Daemon; | ||
use Respect\Deamon\Manager as d; | ||
$d = d::getInstance(); | ||
$d->all(); //list all jobs | ||
$mongodb = $d->get("mongodb"); //get single job | ||
$d->start($mongodb); | ||
$d->status($mongodb); | ||
$d->stop($mongodb); | ||
$d->remove($mongodb); //remove from operating system | ||
$myd = new Deamon\Job("myd"); | ||
$myd->setMain('/usr/bin/my/path'); | ||
$d->save($myd); //save job to operating system | ||
$d->start($myd); | ||
</pre> | ||
|
||
h3. Linux | ||
|
||
Manages upstart (http://upstart.ubuntu.com) scripts and cron jobs. | ||
|
||
h3. Windows | ||
|
||
Manager windows services using the Windows Service Management API (through the | ||
win32service PECL extension). |
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,92 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon\Adapters; | ||
|
||
use Respect\Daemon\Exceptions\DirectoryNotFoundException; | ||
use \UnexpectedValueException; | ||
|
||
class Upstart | ||
{ | ||
|
||
protected $dir; | ||
protected $dirHandle; | ||
|
||
public static function runsOnCurrentEnvironment() | ||
{ | ||
$uname = php_uname(); | ||
if (!stripos($uname, 'linux')) | ||
return false; | ||
return stripos(system('initctl --vesion'), 'upstart'); | ||
} | ||
|
||
public function __construct() | ||
{ | ||
try { | ||
$this->dir = trim(realpath('/etc/init'), DIRECTORY_SEPARATOR); | ||
$this->dirHandle = new DirectoryIterator($this->dir); | ||
} catch (UnexpectedValueException $e) { | ||
throw new DirectoryNotFoundException( | ||
$e->getMessage(), $e->getCode(), $e | ||
); | ||
} | ||
} | ||
|
||
public function all() | ||
{ | ||
$scripts = array(); | ||
foreach ($this->dirHandle as $jobFile) { | ||
if (!$jobFile->isFile()) | ||
continue; | ||
$scripts[] = $jobFile->getFilename(); | ||
} | ||
return $scripts; | ||
} | ||
|
||
public function save(Job $job, $overwrite=false) | ||
{ | ||
file_put_contents( | ||
$this->dir . DIRECTORY_SEPARATOR . $job->getName(), | ||
$this->getDefinition($job) | ||
); | ||
} | ||
|
||
public function remove($jobName) | ||
{ | ||
unlink($this->dir . DIRECTORY_SEPARATOR . $job->getName()); | ||
} | ||
|
||
public function get($jobName) | ||
{ | ||
return $this->getJobFromDefinition( | ||
file_get_contents( | ||
$this->dir . DIRECTORY_SEPARATOR . $job->getName() | ||
) | ||
); | ||
} | ||
|
||
public function status($jobName) | ||
{ | ||
return system("status $jobName"); | ||
} | ||
|
||
public function start($jobName) | ||
{ | ||
return system("start $jobName"); | ||
} | ||
|
||
public function stop($jobName) | ||
{ | ||
return system("stop $jobName"); | ||
} | ||
|
||
protected function getDefinition(Job $job) | ||
{ | ||
|
||
} | ||
|
||
protected function getJobFromDefinition($definition) | ||
{ | ||
|
||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
class EventListener | ||
{ | ||
|
||
protected $action; | ||
protected $event; | ||
|
||
public function __construct($action='', $event='') | ||
{ | ||
$this->action = $action; | ||
$this->event = $event; | ||
} | ||
|
||
public function getAction() | ||
{ | ||
return $this->action; | ||
} | ||
|
||
public function setAction($action) | ||
{ | ||
$this->action = $action; | ||
} | ||
|
||
public function getEvent() | ||
{ | ||
return $this->event; | ||
} | ||
|
||
public function setEvent($event) | ||
{ | ||
$this->event = $event; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
library/Respect/Daemon/Exceptions/DirectoryNotFoundException.php
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,8 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon\Exceptions; | ||
|
||
class DirectoryNotFoundException extends Exception | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
class Executable | ||
{ | ||
|
||
protected $path; | ||
|
||
} |
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,75 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
class Job | ||
{ | ||
|
||
protected $name; | ||
protected $description; | ||
protected $main; | ||
protected $preStart; | ||
protected $postStop; | ||
protected $eventListeners = array(); | ||
|
||
public function __construct($name) | ||
{ | ||
|
||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function getMain() | ||
{ | ||
return $this->main; | ||
} | ||
|
||
public function getPreStart() | ||
{ | ||
return $this->preStart; | ||
} | ||
|
||
public function getPostStop() | ||
{ | ||
return $this->postStop; | ||
} | ||
|
||
public function setDescription($description) | ||
{ | ||
$this->description = $description; | ||
} | ||
|
||
public function setMain(Runnable $main) | ||
{ | ||
$this->main = $main; | ||
} | ||
|
||
public function setPreStart(Script $preStart) | ||
{ | ||
$this->preStart = $preStart; | ||
} | ||
|
||
public function setPostStop(Script $postStop) | ||
{ | ||
$this->postStop = $postStop; | ||
} | ||
|
||
public function addEventListener(Event $event) | ||
{ | ||
$this->eventListeners[spl_object_hash($event)] = $event; | ||
} | ||
|
||
public function removeEventListener(Event $event) | ||
{ | ||
unset($this->eventListeners[spl_object_hash($event)]); | ||
} | ||
|
||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
use Respect\Daemon\Exceptions\DirectoryNotFoundException; | ||
use \UnexpectedValueException; | ||
|
||
class Manager | ||
{ | ||
|
||
public static function getInstance($adapter = null) | ||
{ | ||
if (!is_null($adapter)) { | ||
$adapterName = '\Respect\Daemon\Adapters\\' . $adapter; | ||
return new $adapterName; | ||
} else { | ||
$adapters = static::getAvailableAdapters(); | ||
$adapterName = '\Respect\Daemon\Adapters\\' . array_unshift($adapters); | ||
return new $adapterName; | ||
} | ||
return false; | ||
} | ||
|
||
public static function getAvailableAdapters() | ||
{ | ||
$adapters = array(); | ||
foreach (new DirectoryIterator(__DIR__ . '/Adapter') as $a) { | ||
require_once $a->getPath(); | ||
$adapterName = $a->getBasename('.php'); | ||
$runs = call_user_func( | ||
array( | ||
'\Respect\Daemon\Adapters\\' . $adapterName, | ||
'runsOnCurrentEnvironment' | ||
) | ||
); | ||
if ($runs) | ||
$adapters[] = $adapterName; | ||
} | ||
return $adapters; | ||
} | ||
|
||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
interface Runnable | ||
{ | ||
|
||
public function run(); | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Respect\Daemon; | ||
|
||
class Script | ||
{ | ||
|
||
protected $body; | ||
|
||
} |