Skip to content

Commit

Permalink
TaskRun file logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MUlt1mate committed Apr 10, 2016
1 parent 9c88970 commit 7c601c0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ composer require mult1mate/cron-manager
* Make sure that manager is not publicly available

See also examples for [ActiveRecord](https://github.com/MUlt1mate/cron-manager/tree/master/examples/active_record),
[CodeIgniter](https://github.com/MUlt1mate/cron-manager/tree/master/examples/codeigniter)
and [Yii2](https://github.com/MUlt1mate/cron-manager/tree/master/examples/yii2_basic)

[CodeIgniter](https://github.com/MUlt1mate/cron-manager/tree/master/examples/codeigniter),
[Yii2](https://github.com/MUlt1mate/cron-manager/tree/master/examples/yii2_basic),
[File storage](https://github.com/MUlt1mate/cron-manager/tree/master/examples/file_storage),

## Screenshots

Expand All @@ -64,3 +64,7 @@ and [Yii2](https://github.com/MUlt1mate/cron-manager/tree/master/examples/yii2_b
![Import and export](https://cron.multimate.ru/img/Selection_003.png)

See [Live Demo](https://cron.multimate.ru) for more!

## Changelog

* v1.1.0 - File storage models added
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"name": "mult1mate/cron-manager",
"description": "Flexible cron tasks manager for MVC-type applications",
"minimum-stability": "stable",
"keywords": ["cron","crontab","tasks manager","schedule"],
"keywords": [
"cron",
"crontab",
"tasks manager",
"schedule"
],
"homepage": "https://cron.multimate.ru",
"license": "MIT",
"authors": [
Expand All @@ -15,6 +20,9 @@
"php": ">=5.3.2",
"mtdowling/cron-expression": "1.1.*"
},
"suggest": {
"monolog/monolog": "Allow writing log messages"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"codeclimate/php-test-reporter": "^0.2.0"
Expand Down
6 changes: 6 additions & 0 deletions examples/active_record/models/TaskRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ public function setTs($ts)
$this->ts = $ts;
}

/**
* @return string
*/
public function getOutput()
{
return $this->output;
}

/**
* @param string $output
*/
public function setOutput($output)
{
$this->output = $output;
Expand Down
16 changes: 16 additions & 0 deletions examples/file_storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# PHP Cron tasks manager

## File storage example

Using this models you can store tasks information in file.
This method is **NOT** secure - data file can be modified by anyone.
Integrity is **NOT** guaranteed - new task_id calculated from the last task in file. It's not a problem usually.

Data files store in /tmp/ folder by default.

### Requirements

* PHP 5.3 or above
* [monolog/monolog](https://github.com/Seldaek/monolog)

Monolog uses to write log messages
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use mult1mate\crontab\TaskInterface;

/**
* Model that stores task information in file
* @author mult1mate
* Date: 22.02.16
* Time: 17:33
Expand All @@ -17,11 +18,13 @@ class TaskFile implements TaskInterface
protected $ts_updated;

/**
* Data file. Creates if not exists.
* @var string
*/
protected static $data_file = '/tmp/crontasks';

/**
* Reads file from disk and returns array of tasks
* @return array
*/
public static function taskFileLoad()
Expand All @@ -38,7 +41,8 @@ public static function taskFileLoad()
}

/**
* @param $tasks
* Writes tasks into file
* @param array $tasks
* @return bool
*/
public static function taskFileSave($tasks)
Expand Down Expand Up @@ -70,6 +74,11 @@ public static function getAll()
return self::taskFileLoad();
}

/**
* Returns array of tasks with specified ids
* @param array $task_ids array of task ids
* @return array
*/
public static function find($task_ids)
{
if (!is_array($task_ids)) {
Expand Down Expand Up @@ -136,7 +145,7 @@ public static function createNew()
*/
public function createTaskRun()
{
return new TaskRun();
return new TaskRunFile();
}

/**
Expand Down
141 changes: 141 additions & 0 deletions examples/file_storage/models/TaskRunFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use mult1mate\crontab\TaskRunInterface;

/**
* This model saves task run results into log files
* @author mult1mate
* Date: 10.04.16
* Time: 13:39
*/
class TaskRunFile implements TaskRunInterface
{
protected $task_id;
protected $status;
protected $execution_time;
protected $output;
protected $ts;

/**
* Folder with logs files. Should exists
* @var string
*/
protected $logs_folder = '/tmp/crontasks_logs/';

/**
* Default log file name
* @var string
*/
protected $log_name = 'cron_log.log';

/**
* Writes log in file. Do NOT actually saves the task run
* @return bool
*/
public function saveTaskRun()
{
//if monolog not found does nothing
if (!class_exists('Monolog\Logger')) {
return false;
}
$logger = new Logger('cron_logger');
$logger->pushHandler(new RotatingFileHandler($this->logs_folder . $this->log_name));
$task = TaskFile::taskGet($this->task_id);
if (self::RUN_STATUS_STARTED == $this->status) {
$message = 'task ' . $task->getCommand() . ' just started';
} else {
$message = 'task ' . $task->getCommand() . ' ended with status ' . $this->status
. ', execution time ' . $this->execution_time . ', output: ' . PHP_EOL . $this->output;
}
return $logger->addNotice($message);
}

/**
* @return int
*/
public function getTaskRunId()
{
return 0;
}

/**
* @return int
*/
public function getTaskId()
{
return $this->task_id;
}

/**
* @param int $task_id
*/
public function setTaskId($task_id)
{
$this->task_id = $task_id;
}

/**
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* @param string $status
*/
public function setStatus($status)
{
$this->status = $status;
}

/**
* @return int
*/
public function getExecutionTime()
{
return $this->execution_time;
}

/**
* @param int $execution_time
*/
public function setExecutionTime($execution_time)
{
$this->execution_time = $execution_time;
}

/**
* @return string
*/
public function getTs()
{
return $this->ts;
}

/**
* @param string $ts
*/
public function setTs($ts)
{
$this->ts = $ts;
}

/**
* @return string
*/
public function getOutput()
{
return $this->output;
}

/**
* @param string $output
*/
public function setOutput($output)
{
$this->output = $output;
}
}

0 comments on commit 7c601c0

Please sign in to comment.