Skip to content

Commit

Permalink
First draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Gomes Gaigalas committed Sep 28, 2010
1 parent 548c3e2 commit a9a6558
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.textile
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).
92 changes: 92 additions & 0 deletions library/Respect/Daemon/Adapters/Upstart.php
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)
{

}

}
37 changes: 37 additions & 0 deletions library/Respect/Daemon/EventListener.php
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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Respect\Daemon\Exceptions;

class DirectoryNotFoundException extends Exception
{

}
10 changes: 10 additions & 0 deletions library/Respect/Daemon/Executable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Respect\Daemon;

class Executable
{

protected $path;

}
75 changes: 75 additions & 0 deletions library/Respect/Daemon/Job.php
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)]);
}

}
42 changes: 42 additions & 0 deletions library/Respect/Daemon/Manager.php
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;
}

}
9 changes: 9 additions & 0 deletions library/Respect/Daemon/Runnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Respect\Daemon;

interface Runnable
{

public function run();
}
10 changes: 10 additions & 0 deletions library/Respect/Daemon/Script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Respect\Daemon;

class Script
{

protected $body;

}

0 comments on commit a9a6558

Please sign in to comment.