From 0c85e88aed025f2ff69b583cfc0a3d3ea0e5dc58 Mon Sep 17 00:00:00 2001 From: FrenshT Date: Tue, 25 May 2021 16:36:12 +0100 Subject: [PATCH 1/5] Add Mattermost notifications when the job fail Add Slack notifications when the job fail Add possibility to add a custom email subject --- composer.json | 1 + src/BackgroundJob.php | 43 +++++++++++++++++++++++++-- src/Helper.php | 68 ++++++++++++++++++++++++++++++++++++++++++- src/Jobby.php | 5 ++++ 4 files changed, 113 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 326e27b..acdb02e 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "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..54d72a0 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 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, ]; } From 3968a9d82ea808c8bc18ba8591b61b1e7ce16705 Mon Sep 17 00:00:00 2001 From: FrenshT Date: Tue, 25 May 2021 17:15:59 +0100 Subject: [PATCH 2/5] update Read me --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 433d696..2ca0cca 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,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 ## @@ -208,6 +209,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 +217,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 ## From e07c4717bcaad7ac9a5573f85e0d5122e2ce72dc Mon Sep 17 00:00:00 2001 From: FrenshT Date: Tue, 25 May 2021 18:35:51 +0100 Subject: [PATCH 3/5] change composer.json --- composer.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index acdb02e..f87eec5 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "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": [ @@ -12,6 +12,10 @@ { "name": "Michael Contento", "homepage": "https://github.com/michaelcontento" + }, + { + "name": "Hatim HAFFANE", + "homepage": "https://github.com/hatimox" } ], "require": { From 602e09c1c331762342952e08d54028039bd7bf77 Mon Sep 17 00:00:00 2001 From: FrenshT Date: Wed, 26 May 2021 11:23:44 +0100 Subject: [PATCH 4/5] change the version --- README.md | 14 ++++++-------- composer.json | 10 +--------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2ca0cca..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. @@ -27,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: @@ -37,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 ### diff --git a/composer.json b/composer.json index f87eec5..6e18bb7 100644 --- a/composer.json +++ b/composer.json @@ -4,17 +4,9 @@ "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" } ], From 6f7b8faaa5956f70d73388a49c1e526fcabb81a6 Mon Sep 17 00:00:00 2001 From: hatim haffane Date: Mon, 6 Mar 2023 17:17:43 +0100 Subject: [PATCH 5/5] remove log file if its empty --- src/BackgroundJob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BackgroundJob.php b/src/BackgroundJob.php index 54d72a0..dbc8d6d 100644 --- a/src/BackgroundJob.php +++ b/src/BackgroundJob.php @@ -108,7 +108,7 @@ public function run() // remove log file if empty $logfile = $this->getLogfile(); - if (is_file($logfile) && filesize($logfile) <= 0) { + if (is_file($logfile) && (filesize($logfile) <= 2 || file_get_contents($logfile) == "[]")) { unlink($logfile); } }