diff --git a/README.md b/README.md index 433d696..66d2171 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # Jobby, a PHP cron job manager # -[![Total Downloads](https://img.shields.io/packagist/dt/hellogerard/jobby.svg)](https://packagist.org/packages/hellogerard/jobby) -[![Latest Version](https://img.shields.io/packagist/v/hellogerard/jobby.svg)](https://packagist.org/packages/hellogerard/jobby) -[![Build Status](https://img.shields.io/travis/jobbyphp/jobby.svg)](https://travis-ci.org/jobbyphp/jobby) -[![MIT License](https://img.shields.io/packagist/l/hellogerard/jobby.svg)](https://github.com/jobbyphp/jobby/blob/master/LICENSE) +[![Total Downloads](https://img.shields.io/packagist/dt/hatimox/jobby.svg)](https://packagist.org/packages/hatimox/jobby) +[![Latest Version](https://img.shields.io/packagist/v/hatimox/jobby.svg)](https://packagist.org/packages/hatimox/jobby) +[![Build Status](https://img.shields.io/travis/hatimox/jobby.svg)](https://travis-ci.org/hatimox/jobby) +[![MIT License](https://img.shields.io/packagist/l/hatimox/jobby.svg)](https://github.com/hatimox/jobby/blob/master/LICENSE) Install the master jobby cron job, and it will manage all your offline tasks. Add jobs without modifying crontab. Jobby can handle logging, locking, error emails and more. -**NEW REPO:** We have moved `jobby` to a Github org. Please update your remotes to `https://github.com/jobbyphp/jobby.git`. - ## Features ## - Maintain one master crontab job. @@ -19,6 +17,7 @@ Jobby can handle logging, locking, error emails and more. - Run job as another user, if crontab user has `sudo` privileges. - Run only on certain hostnames (handy in webfarms). - Theoretical Windows support (but not ever tested) +- Send alerts to Slack or Mattermost whenever a job exits with an error status. ## Getting Started ## @@ -26,7 +25,7 @@ Jobby can handle logging, locking, error emails and more. The recommended way to install Jobby is through [Composer](http://getcomposer.org): ``` -$ composer require hellogerard/jobby +$ composer require hatimox/jobby ``` Then add the following line to your (or whomever's) crontab: @@ -36,7 +35,7 @@ Then add the following line to your (or whomever's) crontab: After Jobby installs, you can copy an example file to the project root. ``` -$ cp vendor/hellogerard/jobby/resources/jobby.php . +$ cp vendor/hatimox/jobby/resources/jobby.php . ``` ### Running a job ### @@ -208,6 +207,7 @@ dateFormat | string | Y-m-d H:i:s | Format for da _**Mailing**_ | | | _**Options for emailing errors**_ recipients | string | null | Comma-separated string of email addresses mailer | string | sendmail | Email method: _sendmail_ or _smtp_ or _mail_ +mailSubject | string | null | Email subject smtpHost | string | null | SMTP host, if `mailer` is smtp smtpPort | integer | 25 | SMTP port, if `mailer` is smtp smtpUsername | string | null | SMTP user, if `mailer` is smtp @@ -215,6 +215,11 @@ smtpPassword | string | null | SMTP password smtpSecurity | string | null | SMTP security option: _ssl_ or _tls_, if `mailer` is smtp smtpSender | string | jobby@<hostname> | The sender and from addresses used in SMTP notices smtpSenderName | string | Jobby | The name used in the from field for SMTP messages +_**Notifications**_ | | | _**Options for sending Alerts when errors**_ +mattermostUrl | string | null | The webhook url from Mattermost +slackChannel | string | null | The name of Slack Channel (#channel) +slackUrl | string | null | The webhook url from Slack +slackSender | string | null | The name used in the from field for Slack ## Symfony integration ## diff --git a/composer.json b/composer.json index 326e27b..6e18bb7 100644 --- a/composer.json +++ b/composer.json @@ -1,22 +1,19 @@ { - "name": "hellogerard/jobby", - "homepage": "https://github.com/jobbyphp/jobby", + "name": "hatimox/jobby", + "homepage": "https://github.com/hatimox/jobby", "license": "MIT", "description": "Manage all your cron jobs without modifying crontab. Handles locking, logging, error emails, and more.", "authors": [ { - "name": "Gerard Sychay", - "email": "hellogerard@gmail.com", - "homepage": "https://github.com/hellogerard" - }, - { - "name": "Michael Contento", - "homepage": "https://github.com/michaelcontento" + "name": "Hatim HAFFANE", + "email": "hatim.haffane@gmail.com", + "homepage": "https://github.com/hatimox" } ], "require": { "php": ">=5.6", "dragonmantank/cron-expression": "^3.0", + "maknz/slack": "^1.7", "opis/closure": "^3.5", "swiftmailer/swiftmailer": "^5.4|^6.0", "symfony/process": "^2.7|^3.0|^4.0|^5.0" diff --git a/src/BackgroundJob.php b/src/BackgroundJob.php index c339747..9ebb7fc 100644 --- a/src/BackgroundJob.php +++ b/src/BackgroundJob.php @@ -54,7 +54,11 @@ public function __construct($job, array $config, Helper $helper = null) 'dateFormat' => null, 'enabled' => null, 'haltDir' => null, - 'debug' => null, + 'mattermostUrl' => null, + 'slackChannel' => null, + 'slackUrl' => null, + 'slackSender' => null, + 'mailSubject' => null, ]; $this->config['output_stdout'] = $this->config['output_stdout'] === null ? $this->config['output'] : $this->config['output_stdout']; @@ -73,7 +77,7 @@ public function run() $this->checkMaxRuntime($lockFile); } catch (Exception $e) { $this->log('ERROR: ' . $e->getMessage(), 'stderr'); - $this->mail($e->getMessage()); + $this->notify($e->getMessage()); return; } @@ -96,7 +100,7 @@ public function run() $this->log('INFO: ' . $e->getMessage(), 'stderr'); } catch (Exception $e) { $this->log('ERROR: ' . $e->getMessage(), 'stderr'); - $this->mail($e->getMessage()); + $this->notify($e->getMessage()); } if ($lockAcquired) { @@ -144,6 +148,7 @@ protected function checkMaxRuntime($lockFile) /** * @param string $message + * Deprecated */ protected function mail($message) { @@ -158,6 +163,38 @@ protected function mail($message) ); } + /** + * @param string $message + */ + protected function notify($message) + { + if (!empty($this->config['recipients'])) { + $this->helper->sendMail( + $this->job, + $this->config, + $message + ); + } + + if (!empty($this->config['mattermostUrl'])) { + $this->helper->sendMattermostAlert( + $this->job, + $this->config, + $message + ); + } + + if (!empty($this->config['slackChannel']) && !empty($this->config['slackUrl'])) { + $this->helper->sendSlackAlert( + $this->job, + $this->config, + $message + ); + } + + + } + /** * @param string $output * @return string @@ -246,7 +283,9 @@ protected function runFunction() } $content = ob_get_contents(); if ($logfile = $this->getLogfile()) { - file_put_contents($this->getLogfile(), $content, FILE_APPEND); + if(strlen($content) > 2){ + file_put_contents($this->getLogfile(), $content, FILE_APPEND); + } } ob_end_clean(); diff --git a/src/Helper.php b/src/Helper.php index 272b4e3..35a459b 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -1,6 +1,8 @@ mailer = $mailer; + $this->guzzle = new Guzzle; } /** @@ -51,7 +62,11 @@ public function sendMail($job, array $config, $message) EOF; $mail = new \Swift_Message(); $mail->setTo(explode(',', $config['recipients'])); - $mail->setSubject("[$host] '{$job}' needs some attention!"); + if(empty($config['mailSubject'])){ + $mail->setSubject("[$host] '{$job}' needs some attention!"); + }else{ + $mail->setSubject($config['mailSubject']); + } $mail->setBody($body); $mail->setFrom([$config['smtpSender'] => $config['smtpSenderName']]); $mail->setSender($config['smtpSender']); @@ -249,4 +264,55 @@ public function getSystemNullDevice() } return 'NUL'; } + + /** + * @param string $job + * @param array $config + * @param string $message + * + * @return void + */ + public function sendSlackAlert($job, array $config, $message) + { + $host = $this->getHost(); + $body = <<to($config['slackChannel']); + if($config['slackSender']){ + $client->from($config['slackSender']); + } + $client->send($body); + + } + + /** + * @param string $job + * @param array $config + * @param string $message + * + * @return void + */ + public function sendMattermostAlert($job, array $config, $message) + { + $host = $this->getHost(); + $body = <<$body]; + $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); + $this->guzzle->post($config['mattermostUrl'], ['body' => $encoded]); + + } } diff --git a/src/Jobby.php b/src/Jobby.php index 821f8c6..30007e9 100644 --- a/src/Jobby.php +++ b/src/Jobby.php @@ -79,6 +79,11 @@ public function getDefaultConfig() 'enabled' => true, 'haltDir' => null, 'debug' => false, + 'mattermostUrl' => null, + 'slackChannel' => null, + 'slackUrl' => null, + 'slackSender' => null, + 'mailSubject' => null, ]; }