Skip to content

Commit

Permalink
Share tested classes from live project
Browse files Browse the repository at this point in the history
  • Loading branch information
Waldz committed Jun 30, 2017
1 parent a3c6bbd commit 48c9715
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# PHP multi-OS daemon library

Create long-running jobs, daemons, self recovering processes for any OS environment
Create long-running jobs, daemons, self recovering processes for any OS environment

Install library via Composer
------------
```sh
composer require waldz/php-multios-daemon
```
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "waldz/php-multios-daemon",
"description": "Create long-running jobs, daemons, self recovering processes for any OS environment",
"license": "MIT",
"keywords": [
"php",
"daemon",
"cronjob"
],
"authors": [
{
"name": "Valdas Petrulis",
"email": "petrulis.valdas@gmail.com",
"homepage": "https://lt.linkedin.com/in/valdaspetrulis",
"role": "Architect/Developer/DevOps"
}
],
"type": "library",
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"MultiOSDaemon\\": "src"
}
},
"require": {
"php": ">=5.3.0"
}
}

76 changes: 76 additions & 0 deletions src/CronAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace MultiOSDaemon;

/**
* Run any process as a CRON job - run only once in minute and do some job
*
* @package Daemon
* @subpackage Model
*
* @author Valdas Petrulis <petrulis.valdas@gmail.com>
*/
abstract class CronAbstract
extends DaemonAbstract
{

/**
* Daemon object constructor
*
* @param string $pidFile Daemon process id file
* @param string $statusFile Daemon status file
* @param string $logFile Daemon log file
*/
public function __construct($pidFile, $statusFile, $logFile=null)
{
parent::__construct($pidFile, $statusFile, $logFile);

// Run only once in a minute and exit
$this->setCheckTtl(null);
$this->setRestartTtl(0);

}

/**
* Configures daemon job before looping
*
* @param array $jobParams
* @return void
*/
public function init($jobParams = array())
{
// Do nothing
}

/**
* On loop cycle of daemon
*
* @return void
*/
public function step()
{
if($this->checkJobTime()) {
$this->doJob();
} else {
$this->log('CRON time not come yet');
}
}

/**
* Check if time has come for job to be run
*
* @return bool
*/
public function checkJobTime()
{
return true;
}

/**
* On time then CRON need to be run
*
* @return void
*/
abstract public function doJob();

}
Loading

0 comments on commit 48c9715

Please sign in to comment.