Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 40 additions & 3 deletions src/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -144,6 +148,7 @@ protected function checkMaxRuntime($lockFile)

/**
* @param string $message
* Deprecated
*/
protected function mail($message)
{
Expand All @@ -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
Expand Down
68 changes: 67 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Jobby;

use GuzzleHttp\Client as Guzzle;

class Helper
{
/**
Expand All @@ -23,12 +25,21 @@ class Helper
*/
private $mailer;

/**
* The Guzzle HTTP client instance
*
* @var \GuzzleHttp\Client
*/
protected $guzzle;


/**
* @param \Swift_Mailer $mailer
*/
public function __construct(\Swift_Mailer $mailer = null)
{
$this->mailer = $mailer;
$this->guzzle = new Guzzle;
}

/**
Expand All @@ -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']);
Expand Down Expand Up @@ -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 = <<<EOF
$message

You can find its output in {$config['output']} on $host.

Best,
jobby@$host
EOF;
$client = new \Maknz\Slack\Client($config['slackUrl']);
$client->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 = <<<EOF
$message

You can find its output in {$config['output']} on $host.

Best,
jobby@$host
EOF;
$payload = ['text'=>$body];
$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
$this->guzzle->post($config['mattermostUrl'], ['body' => $encoded]);

}
}
5 changes: 5 additions & 0 deletions src/Jobby.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function getDefaultConfig()
'enabled' => true,
'haltDir' => null,
'debug' => false,
'mattermostUrl' => null,
'slackChannel' => null,
'slackUrl' => null,
'slackSender' => null,
'mailSubject' => null,
];
}

Expand Down