Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur LORENT committed Sep 11, 2020
1 parent 4821450 commit 436463e
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog

## [1.1.0](https://github.com/Okipa/laravel-supervisor-downtime-notifier/compare/1.0.2...1.1.0)

2020-09-11

* Added the `supervisor:downtime:simulate` command to simulate supervisor downtime detection and check if everything is correctly configured.

## [1.0.0](https://github.com/Okipa/laravel-supervisor-downtime-notifier/releases/tag/1.0.0)

2020-05-13
2019-11-08

* First release.
57 changes: 44 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ Get notified and execute PHP callback when:

Notifications can be sent by mail, Slack and webhooks (chats often provide a webhook API).

**:warning: Package in development :warning:**

## Compatibility

| Laravel version | PHP version | Package version |
|---|---|---|
| ^6.0 | ^7.4 | ^1.0 |

## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Testing](#testing)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Credits](#credits)
- [Licence](#license)
* [Requirements](#requirements)
* [Installation](#installation)
* [Configuration](#configuration)
* [Translations](#translations)
* [Usage](#usage)
* [Testing](#testing)
* [Changelog](#changelog)
* [Contributing](#contributing)
* [Credits](#credits)
* [Licence](#license)

## Requirements

Expand All @@ -40,7 +43,7 @@ The user running PHP CLI will execute the following commands:
* `systemctl is-active supervisor`
* `supervisorctl status "<your-process-name>"`

As so, make sure you give him permission to execute these actions (`sudo visudo -f /etc/sudoers.d/<user>`) :
As so, make sure you give him permission to execute these actions (`sudo visudo -f /etc/sudoers.d/<user>`):

* `<user> ALL=NOPASSWD:/bin/systemctl is-active supervisor`
* `<user> ALL=NOPASSWD:/usr/bin/supervisorctl status *`
Expand Down Expand Up @@ -69,16 +72,44 @@ Publish the package configuration:
php artisan vendor:publish --tag=supervisor-downtime-notifier:config
```

## Translations

All words and sentences used in this package are translatable.

See how to translate them on the Laravel official documentation: https://laravel.com/docs/localization#using-translation-strings-as-keys.

Here is the list of the words and sentences available for translation by default:

```text
* [:app - :env] supervisor service is not started
* We have detected that the supervisor service is not started on [:app - :env](:url).
* `[:app - :env]` supervisor service is not started on :url.
* Supervisor service is not started.
* {1}[:app - :env] :count supervisor down process has been detected|[2,*][:app - :env] :count supervisor down processes have been detected
* {1}We have detected :count supervisor down process on [:app - :env](:url): ":processes".|[2,*]We have detected :count supervisor down processes on [:app - :env](:url): ":processes".
* Please check your down processes connecting to your server and executing the "supervisorctl status" command.
* {1}`[:app - :env]` :count supervisor down process has been detected on :url: ":processes".|[2,*]`[:app - :env]` :count supervisor down processes have been detected on :url: ":processes".
* {1}Down supervisor process detected: ":processes".|[2,*]Down supervisor processes detected: ":processes".
* Notification test:
* Exception test:
```

## Usage

Just add this command in the `schedule()` method of your `\App\Console\Kernel` class :
Just add this command in the `schedule()` method of your `\App\Console\Kernel` class:

```php
$schedule->command('supervisor:downtime:notify')->everyFifteenMinutes();
```

And you will be notified if your supervisor service is not running, or if your environment supervisor processes are down when the command will be executed.

To check if everything is correctly configured, you can simulate supervisor downtime detection:

```bash
php artisan supervisor:downtime:simulate
```

## Testing

```bash
Expand All @@ -95,8 +126,8 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Credits

- [Arthur LORENT](https://github.com/okipa)
- [All Contributors](../../contributors)
* [Arthur LORENT](https://github.com/okipa)
* [All Contributors](../../contributors)

## License

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"require": {
"php": "^7.4",
"illuminate/support": "^6.0||^7.0",
"laravel-notification-channels/webhook": "^2.0",
"laravel-notification-channels/webhook": "2.0.*",
"laravel/slack-notification-channel": "^2.0"
},
"require-dev": {
Expand Down
128 changes: 64 additions & 64 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Laravel and similar phpmd ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Written using this resource : https://phpmd.org/rules/index.html</description>
<!-- clean code rules -->
<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess"/>
<exclude name="ElseExpression"/>
<exclude name="BooleanArgumentFlag"/>
</rule>
<!-- code size rules -->
<rule ref="rulesets/codesize.xml">
<exclude name="TooManyPublicMethods"/>
<exclude name="TooManyMethods"/>
<exclude name="ExcessiveClassComplexity"/>
</rule>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="maxmethods" value="20"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="maxmethods" value="30"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<property name="maximum" value="60"/>
</properties>
</rule>
<!-- controversial rules -->
<rule ref="rulesets/controversial.xml"/>
<!-- design rules -->
<rule ref="rulesets/design.xml">
<exclude name="CouplingBetweenObjects"/>
<exclude name="NumberOfChildren"/>
</rule>
<!-- naming rules -->
<rule ref="rulesets/naming.xml">
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Written using this resource : https://phpmd.org/rules/index.html</description>
<!-- clean code rules -->
<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess"/>
<exclude name="ElseExpression"/>
<exclude name="BooleanArgumentFlag"/>
</rule>
<!-- code size rules -->
<rule ref="rulesets/codesize.xml">
<exclude name="TooManyPublicMethods"/>
<exclude name="TooManyMethods"/>
<exclude name="ExcessiveClassComplexity"/>
</rule>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="maxmethods" value="20"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="maxmethods" value="30"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<property name="maximum" value="60"/>
</properties>
</rule>
<!-- controversial rules -->
<rule ref="rulesets/controversial.xml"/>
<!-- design rules -->
<rule ref="rulesets/design.xml">
<exclude name="CouplingBetweenObjects"/>
<exclude name="NumberOfChildren"/>
</rule>
<!-- naming rules -->
<rule ref="rulesets/naming.xml">
<exclude name="LongClassName"/>
<exclude name="ShortVariable"/>
<exclude name="LongVariable"/>
<exclude name="ShortMethodName"/>
<exclude name="BooleanGetMethodName"/>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable"
since="0.2"
message="Avoid variables with short names like {0}. Configured minimum length is {1}."
class="PHPMD\Rule\Naming\ShortVariable"
externalInfoUrl="http://phpmd.org/rules/naming.html#shortvariable">
<priority>3</priority>
<properties>
<property name="minimum" description="Minimum length for a variable, property or parameter name" value="3"/>
<property name="exceptions" value="id,js,uu,ii,oo"/>
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
<properties>
<property name="exceptions" value="up,to,at"/>
</properties>
</rule>
<!-- unused code rules -->
<rule ref="rulesets/unusedcode.xml"/>
<exclude name="ShortVariable"/>
<exclude name="LongVariable"/>
<exclude name="ShortMethodName"/>
<exclude name="BooleanGetMethodName"/>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable"
since="0.2"
message="Avoid variables with short names like {0}. Configured minimum length is {1}."
class="PHPMD\Rule\Naming\ShortVariable"
externalInfoUrl="http://phpmd.org/rules/naming.html#shortvariable">
<priority>3</priority>
<properties>
<property name="minimum" description="Minimum length for a variable, property or parameter name" value="3"/>
<property name="exceptions" value="id,js,uu,ii,oo"/>
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
<properties>
<property name="exceptions" value="up,to,at"/>
</properties>
</rule>
<!-- unused code rules -->
<rule ref="rulesets/unusedcode.xml"/>
</ruleset>
13 changes: 9 additions & 4 deletions src/Callbacks/OnDownProcesses.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ class OnDownProcesses
{
/**
* @param \Illuminate\Support\Collection $downProcesses
* @param bool $isTesting
*
* @throws \Okipa\LaravelSupervisorDowntimeNotifier\Exceptions\SupervisorDownProcessesDetected
*/
public function __invoke(Collection $downProcesses)
public function __invoke(Collection $downProcesses, bool $isTesting = false)
{
// triggers an exception to make your monitoring tool (Sentry, ...) aware of the problem.
throw new SupervisorDownProcessesDetected($downProcesses->count() > 1
? 'Down supervisor processes detected: "' . $downProcesses->implode('", "') . '".'
: 'Down supervisor process detected: "' . $downProcesses->first() . '".');
throw new SupervisorDownProcessesDetected(($isTesting ? (string) __('Exception test:') . ' ' : '')
. (string) trans_choice(
'{1}Down supervisor process detected: ":processes".'
. '|[2,*]Down supervisor processes detected: ":processes".',
$downProcesses->count(),
['processes' => $downProcesses->implode('", "')]
));
}
}
6 changes: 2 additions & 4 deletions src/Callbacks/OnServiceNotStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

class OnServiceNotStarted
{
/**
* @throws \Okipa\LaravelSupervisorDowntimeNotifier\Exceptions\SupervisorServiceNotStarted
*/
/** @throws \Okipa\LaravelSupervisorDowntimeNotifier\Exceptions\SupervisorServiceNotStarted */
public function __invoke()
{
// triggers an exception to make your monitoring tool (Sentry, ...) aware of the problem.
throw new SupervisorServiceNotStarted('Supervisor service is not started.');
throw new SupervisorServiceNotStarted(__('Supervisor service is not started.'));
}
}
35 changes: 35 additions & 0 deletions src/Commands/SimulateSupervisorDownTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Okipa\LaravelSupervisorDowntimeNotifier\Commands;

use Illuminate\Console\Command;
use Okipa\LaravelSupervisorDowntimeNotifier\SupervisorDowntimeNotifier;

class SimulateSupervisorDownTime extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'supervisor:downtime:simulate';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Simulate supervisor downtime for testing purpose.';

/** @throws \Okipa\LaravelSupervisorDowntimeNotifier\Exceptions\SupervisorDownProcessesDetected */
public function handle(): void
{
$fakeDownProcesses = collect([[],[]]);
$notification = (new SupervisorDowntimeNotifier)->getDownProcessesNotification($fakeDownProcesses, true);
(new SupervisorDowntimeNotifier)->getNotifiable()->notify($notification);
$onDownProcesses = (new SupervisorDowntimeNotifier)->getDownProcessesCallback();
if ($onDownProcesses) {
$onDownProcesses($fakeDownProcesses, true);
}
}
}
Loading

0 comments on commit 436463e

Please sign in to comment.