Skip to content

Commit

Permalink
Merge pull request #1 from ingenerator/initial-extract
Browse files Browse the repository at this point in the history
Initial extraction from donor project
  • Loading branch information
craig410 authored Feb 6, 2024
2 parents 8a4f9be + 3be831c commit 1b49bee
Show file tree
Hide file tree
Showing 49 changed files with 4,810 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Run tests
on:
push:
branches:
# Only mainline branches, features etc are covered on the pull_request trigger
- '*.x'
pull_request:

jobs:
run-tests:
runs-on: ubuntu-latest
name: Run tests
strategy:
fail-fast: false
matrix:
php_version:
- '8.2'
dependencies:
- 'default'
include:
- php_version: '8.2'
dependencies: 'lowest'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
tools: composer:v2

- name: Checkout
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ matrix.dependencies }}
- name: Install composer dependencies
env:
DEPENDENCIES: ${{ matrix.dependencies }}
run: |
if [ $DEPENDENCIES == 'lowest' ]
then
composer update --prefer-lowest --no-interaction --no-suggest --no-progress
else
composer install --no-interaction --no-suggest --no-progress
fi
- name: Run unit tests
run: |
vendor/bin/phpunit --testsuite=unit --verbose
- name: Start MySQL server
run: |
sudo systemctl start mysql.service
- name: Run integration tests
run: |
vendor/bin/phpunit --testsuite=integration --verbose
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/vendor/
/composer.lock
.phpunit.result.cache
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Unreleased

## v1.0.0 (2024-02-06)

* Initial version
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2024, inGenerator Ltd
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 changes: 41 additions & 0 deletions bin/cronjob-runner
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env php
<?php

use Ingenerator\ScheduledTaskRunner\CronjobRunnerFactory;


ini_set('display_errors', 1);
error_reporting(E_ALL);
set_error_handler(
function ($code, $msg, $file, $line) {
throw new ErrorException($msg, 0, $code, $file, $line);
}
);

try {
$factory = require_once($argv[1]);
if ( ! $factory instanceof CronjobRunnerFactory) {
throw new RuntimeException('You must provide a script which returns a CronjobRunnerFactory as the first arg');
}

$controller = $factory->getController();

pcntl_async_signals(TRUE);
pcntl_signal(SIGTERM, fn() => $controller->signalTermination());

$controller->execute();

} catch (Throwable $e) {
fwrite(
STDERR,
sprintf(
"Fatal error executing %s- [%s] %s (%s:%s)\n",
__FILE__,
get_class($e),
$e->getMessage(),
$e->getFile(),
$e->getLine()
)
);
exit(1);
}
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "ingenerator/scheduled-task-runner",
"homepage": "https://github.com/ingenerator/scheduled-task-runner",
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Andrew Coulton",
"email": "andrew@ingenerator.com"
},
{
"name": "Craig Gosman",
"email": "craig@ingenerator.com"
}
],
"minimum-stability": "stable",
"bin": [
"bin/cronjob-runner"
],
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"support": {
"source": "https://github.com/ingenerator/scheduled-task-runner",
"issues": "https://github.com/ingenerator/scheduled-task-runner/issues"
},
"require": {
"php": "~8.2.0",
"ext-pcntl": "*",
"ext-pdo": "*",
"dragonmantank/cron-expression": "^3.3.2",
"ingenerator/php-utils": "^2.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/lock": "^5.4 || ^6.0 || ^7.0",
"symfony/process": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.5"
},
"autoload": {
"psr-4": {
"Ingenerator\\ScheduledTaskRunner\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"test\\integration\\Ingenerator\\ScheduledTaskRunner\\": "test/integration",
"test\\mock\\Ingenerator\\ScheduledTaskRunner\\": "test/mock",
"test\\unit\\Ingenerator\\ScheduledTaskRunner\\": "test/unit"
}
}
}
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!-- Config file for PHPUnit automated tests -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
beStrictAboutChangesToGlobalState="true"
bootstrap="test/phpunit-bootstrap.php"
cacheResultFile="test/.phpunit.result.cache"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>test/unit</directory>
</testsuite>
<testsuite name="integration">
<directory>test/integration</directory>
</testsuite>
</testsuites>
</phpunit>
36 changes: 36 additions & 0 deletions src/CronConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Ingenerator\ScheduledTaskRunner;

class CronConfigLoader
{
/**
* @var CronTaskGroupDefinition[]
*/
private array $definitions;

public function __construct(array $config)
{
$this->definitions = [];
foreach ($config as $group_name => $values) {
$values['group_name'] = $group_name;
$this->definitions[] = CronTaskGroupDefinition::fromArray($values);
}
}

/**
* @return CronTaskGroupDefinition[]
*/
public function getAllTaskDefinitions(): array
{
return $this->definitions;
}

/**
* @return CronTaskGroupDefinition[]
*/
public function getActiveTaskDefinitions(): array
{
return array_values(array_filter($this->definitions, fn (CronTaskGroupDefinition $d) => $d->isEnabled()));
}
}
Loading

0 comments on commit 1b49bee

Please sign in to comment.