Skip to content

Commit

Permalink
Merge pull request #2 from pluswerk/throttle-function
Browse files Browse the repository at this point in the history
added throttle function and replaced exception with warning log
  • Loading branch information
CamillH authored Oct 27, 2022
2 parents 059aa7e + 1d5a5ff commit cb21ff3
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 20 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tasks

on: [push, pull_request]

jobs:
lint-php:
name: "php: ${{ matrix.php }}"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ '7.2', '7.3', '7.4', '8.0', '8.1' ]
steps:
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.php }}-composer-
- run: composer install --no-interaction --no-progress
- run: ./vendor/bin/grumphp run --ansi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# uptime-robot

This package simply wraps another PHP function to create and send a heartbeat for uptime-robot and throws an exception on error.
This package wraps another PHP function to create and send a heartbeat for uptime-robot and can log a warning if request was not succesful.

## Upgrade from 1 to 2

there is no `HeartBeatException` anymore. If you want to log that the request to uptime-robot has failed, you need to inject an `\Psr\Log\LoggerInterface` in the constructor.

## Installation

```
composer req pluswerk/uptime-robot
```

## Usage
Send request to UptimeRobot:
```php
$heartBeat = new HeartBeat();
$heartBeat->alive('https://heartbeat.uptimerobot.com/m0000000000-0000000000000000000000000');
```

Throttle your requests with a given time:

```php
$heartBeat->throttledAlive($yourUptimeUrl, 300);
```
20 changes: 19 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
}
},
"require" : {
"php" : "^7.2"
"php" : "^7.2 || ~8.0 || ~8.1",
"psr/log": "^1 || ^2 || ^3"
},
"require-dev": {
"pluswerk/grumphp-config": "^4.0 || ^5.0"
},
"config": {
"allow-plugins": {
"phpro/grumphp": true,
"pluswerk/grumphp-config": true
}
},
"extra": {
"pluswerk/grumphp-config": {
"auto-setting": true
},
"grumphp": {
"config-default-path": "vendor/pluswerk/grumphp-config/grumphp.yml"
}
}
}
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#2 \\$associative of function get_headers expects bool, int\\|false given\\.$#"
count: 1
path: src/HeartBeat.php
-
message: "#^Parameter \\#2 \\$format of function get_headers expects int, int\\|false given\\.$#"
count: 1
path: src/HeartBeat.php
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon
parameters:
level: max
reportUnmatchedIgnoredErrors: false
paths:
- src
10 changes: 0 additions & 10 deletions src/Exception/HeartBeatException.php

This file was deleted.

36 changes: 28 additions & 8 deletions src/HeartBeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace Pluswerk\UptimeRobot;

use Pluswerk\UptimeRobot\Exception\HeartBeatException;
use Psr\Log\LoggerInterface;

class HeartBeat
{
/**
* @param string $url
* @throws \Pluswerk\UptimeRobot\Exception\HeartBeatException
*/
/** @var int */
private $lastThrottledExecution = 0;
/** @var LoggerInterface|null */
private $logger = null;

public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

public function alive(string $url): void
{
$context = stream_context_set_default(
Expand All @@ -21,9 +27,13 @@ public function alive(string $url): void
],
]
);
$headers = @get_headers($url, 0, $context);

$headers = @get_headers($url, PHP_MAJOR_VERSION >= 8 ? false : 0, $context);
if (false === $headers) {
throw new HeartBeatException('HeartBeat error occurred sending alive');
if ($this->logger) {
$this->logger->warning('HeartBeat error occurred sending alive');
}
return;
}

foreach ($headers as $header) {
Expand All @@ -32,6 +42,16 @@ public function alive(string $url): void
}
}

throw new HeartBeatException('HeartBeat alive did not receive proper http headers from uptime-robot');
if ($this->logger) {
$this->logger->warning('HeartBeat alive did not receive proper http headers from uptime-robot');
}
}

public function throttledAlive(string $url, int $throttleTime): void
{
if (time() > $this->lastThrottledExecution + $throttleTime) {
$this->alive($url);
$this->lastThrottledExecution = time();
}
}
}

0 comments on commit cb21ff3

Please sign in to comment.