Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MUlt1mate committed Jan 7, 2016
1 parent 9704ee3 commit d7651be
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 12 deletions.
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,55 @@

This is a flexible tasks manager designed for MVC-type applications. It's used instead of standard linux *crontab* command.

The purpose of this tool is to provide an easy way to manipulate repetitive tasks.
The purpose of this tool is to provide an easy way to manipulate repetitive tasks.

[Live Demo](https://cron.multimate.ru)

## How this is works
Replace all tasks in crontab file with one which will invoke method ```TaskManager::checkAndRunTasks()```.

Import tasks from current crontab file or add them manually. Active tasks will run one by one if current time matches with the task's time expression. Output of tasks can be handled. For each execution will be assigned status:
* **Success** if method returned ```true```;
* **Error** if method returned ```false``` or an exception was caught;
* **Started** if task is running or wasn't ended properly.

### Features
* Works with any storage engines
* Flexible implementation with interfaces
* Disable, enable and run tasks through tool interface
* Handle tasks output however you want
* Time expression helper shows next run dates
* Monitor runs results
* Export and import tasks from crontab
* Add needed method for new task from dropdown

## Installation

Install package via Composer
```
composer require mult1mate/cron-manager
```

### Requirements

* PHP 5.4 or above
* [mtdowling/cron-expression](https://github.com/mtdowling/cron-expression)

### Configure
* Create tables if you want to store data in database (SQL queries in `DbHelper` class)
* Implement `TaskInterface` and `TaskRunInterface` or use predefined classes from the Example folder
* Copy and modify controller and views. Or create your own.
* Import tasks through interface or add them manually
* Add new line into crontab file that will invoke ```TaskManager::checkAndRunTasks()```
* Disable tasks that will be invoke through the manager
* Make sure that manager is not publicly available


## Screenshots

![Tasks list](https://cron.multimate.ru/img/Selection_006.png)
![Report](https://cron.multimate.ru/img/Selection_008.png)
![Logs](https://cron.multimate.ru/img/Selection_007.png)
![Import and export](https://cron.multimate.ru/img/Selection_003.png)

See [Live Demo](https://cron.multimate.ru) for more!
12 changes: 9 additions & 3 deletions examples/active_record/Controllers/TasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*/
class TasksController extends BaseController
{
const CONTROLLERS_FOLDER = __DIR__;

public function index()
{
$this->renderView('tasks_list', [
'tasks' => Task::getList(),
'methods' => TaskManager::getAllMethods(__DIR__),
'methods' => TaskManager::getAllMethods(self::CONTROLLERS_FOLDER),
]);
}

Expand Down Expand Up @@ -75,6 +77,10 @@ public function getDates()
{
$time = $_POST['time'];
$dates = TaskManager::getRunDates($time);
if (empty($dates)) {
echo 'Invalid expression';
return;
}
echo '<ul>';
foreach ($dates as $d)
/**
Expand Down Expand Up @@ -112,7 +118,7 @@ public function taskEdit()

$this->renderView('task_edit', [
'task' => $task,
'methods' => TaskManager::getAllMethods(__DIR__),
'methods' => TaskManager::getAllMethods(self::CONTROLLERS_FOLDER),
]);
}

Expand All @@ -137,7 +143,7 @@ public function tasksUpdate()

public function checkTasks()
{
TaskManager::checkTasks(Task::getAll());
TaskManager::checkAndRunTasks(Task::getAll());
}

public function tasksReport()
Expand Down
3 changes: 2 additions & 1 deletion examples/active_record/models/Task.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use ActiveRecord\Model;
use mult1mate\crontab\DbHelper;
use mult1mate\crontab\TaskInterface;
use mult1mate\crontab\TaskRunInterface;
Expand All @@ -16,7 +17,7 @@
* @property \ActiveRecord\DateTime $ts
* @property \ActiveRecord\DateTime $ts_updated
*/
class Task extends \ActiveRecord\Model implements TaskInterface
class Task extends Model implements TaskInterface
{
static $has_many = [
['taskruns', 'class_name' => 'TaskRun']
Expand Down
3 changes: 2 additions & 1 deletion examples/active_record/models/TaskRun.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use ActiveRecord\Model;
use mult1mate\crontab\TaskRunInterface;

/**
Expand All @@ -13,7 +14,7 @@
* @property Task $task
* @property \ActiveRecord\DateTime $ts
*/
class TaskRun extends \ActiveRecord\Model implements TaskRunInterface
class TaskRun extends Model implements TaskRunInterface
{
static $belongs_to = [
['task']
Expand Down
Binary file modified src/DbHelper.php
Binary file not shown.
58 changes: 52 additions & 6 deletions src/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static function editTask($task, $time_expression, $command, $status = Tas
return $task;
}

/**
* @param string $command
* @return string
* @throws CrontabManagerException
*/
public static function validateCommand($command)
{
list($class, $method, $args) = self::parseCommand($command);
Expand All @@ -41,8 +46,13 @@ public static function validateCommand($command)
return $class . '::' . $method . '(' . trim(implode(',', $args), ',') . ')';
}

public static function checkTasks($tasks)
/**
* Runs active tasks if current time matches with time expression
* @param array $tasks
*/
public static function checkAndRunTasks($tasks)
{
$date = date('Y-m-d H:i:s');
foreach ($tasks as $t) {
/**
* @var TaskInterface $t
Expand All @@ -51,11 +61,18 @@ public static function checkTasks($tasks)
continue;

$cron = CronExpression::factory($t->getTime());
if ($cron->isDue())

if ($cron->isDue($date))
self::runTask($t);
}
}

/**
* Returns next run dates for time expression
* @param string $time
* @param int $count
* @return array
*/
public static function getRunDates($time, $count = 10)
{
try {
Expand All @@ -68,6 +85,7 @@ public static function getRunDates($time, $count = 10)
}

/**
* Runs task and returns output
* @param TaskInterface $task
* @return string
*/
Expand Down Expand Up @@ -99,6 +117,10 @@ public static function runTask($task)
return $output;
}

/**
* @param string $command
* @return mixed
*/
public static function parseAndRunCommand($command)
{
try {
Expand All @@ -118,6 +140,11 @@ public static function parseAndRunCommand($command)
return $result;
}

/**
* @param string $command
* @return array
* @throws CrontabManagerException
*/
protected static function parseCommand($command)
{
if (preg_match('/(\w+)::(\w+)\((.*)\)/', $command, $match)) {
Expand All @@ -131,6 +158,12 @@ protected static function parseCommand($command)
throw new CrontabManagerException('Command not recognized');
}

/**
* Returns all public methods for class
* @param string $class
* @return array
* @throws CrontabManagerException
*/
public static function getControllerMethods($class)
{
if (!class_exists($class))
Expand All @@ -145,7 +178,13 @@ public static function getControllerMethods($class)
return ($result_methods);
}

public static function getControllersList($paths)
/**
* Returns names of all php files in directories
* @param array $paths
* @return array
* @throws CrontabManagerException
*/
protected static function getControllersList($paths)
{
$controllers = [];
foreach ($paths as $p) {
Expand All @@ -160,6 +199,12 @@ public static function getControllersList($paths)
return $controllers;
}

/**
* Scan folders for classes and return all their public methods
* @param string|array $folder
* @return array
* @throws CrontabManagerException
*/
public static function getAllMethods($folder)
{
if (!is_array($folder))
Expand Down Expand Up @@ -225,10 +270,11 @@ public static function parseCrontab($cron, $task_class)
}

/**
* Formats task line for export
* @param TaskInterface $task
* @param $path
* @param $php_bin
* @param $input_file
* @param string $path
* @param string $php_bin
* @param string $input_file
* @return string
*/
public static function getTaskCrontabLine($task, $path, $php_bin, $input_file)
Expand Down

0 comments on commit d7651be

Please sign in to comment.