Skip to content

Commit f5022c2

Browse files
author
Eugene Leonovich
committed
Add files
0 parents  commit f5022c2

34 files changed

+3960
-0
lines changed

.env.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Default values for any environment variables referenced in the Compose file
2+
# https://docs.docker.com/compose/environment-variables/#setting-environment-variables-with-docker-compose-run
3+
4+
TARANTOOL_USER_NAME=jobserver
5+
TARANTOOL_USER_PASSWORD=jobserver

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/app/config/jobserver_config.lua
2+
/app/config/local.php
3+
/var/log/
4+
/vendor/
5+
/.env
6+
/.php_cs
7+
/docker-compose.override.yml
8+
/phpunit.xml

.php_cs.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
->exclude('docker')
6+
->exclude('var')
7+
;
8+
9+
return PhpCsFixer\Config::create()
10+
->setFinder($finder)
11+
->setRiskyAllowed(true)
12+
->setRules([
13+
'@Symfony' => true,
14+
'@Symfony:risky' => true,
15+
'array_syntax' => ['syntax' => 'short'],
16+
'no_useless_else' => true,
17+
'no_useless_return' => true,
18+
'ordered_imports' => true,
19+
'phpdoc_order' => true,
20+
'php_unit_strict' => true,
21+
'strict_comparison' => true,
22+
])
23+
->setCacheFile(__DIR__.'/var/.php_cs.cache')
24+
;

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Eugene Leonovich
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# JobServer
2+
3+
JobServer is a skeleton repository used for creating background jobs.
4+
It contains the minimal configuration files and folders you will need for quick start from scratch.
5+
6+
7+
## Setup
8+
9+
First, create your own `docker-compose.override.yml` file by copying
10+
the [docker-compose.override.yml.dist](docker-compose.override.yml.dist) file and customize to your needs.
11+
Do the same for all `*.dist` files located in [app/config](app/config).
12+
13+
Then, browse to the project directory and execute this command:
14+
15+
```bash
16+
docker-compose up -d
17+
```
18+
19+
20+
## Run cli
21+
22+
```bash
23+
docker-compose exec worker ./jobserver
24+
```
25+
26+
27+
## Put jobs into the "default" queue
28+
29+
```bash
30+
docker-compose exec worker ./jobserver queue:put default '{"payload": {"service": "greet", "args": {"name": "foobar"}}}' -H tarantool
31+
```
32+
33+
34+
## Debug cli
35+
36+
```bash
37+
docker-compose exec worker bash -c 'PHP_IDE_CONFIG="serverName=jobserver.dev" ./jobserver'
38+
```
39+
40+
To debug the job runner, run
41+
42+
```bash
43+
docker-compose run --rm worker bash -c ' \
44+
PHP_IDE_CONFIG="serverName=jobserver.dev" \
45+
TNT_JOBQUEUE_PASSWORD=jobserver \
46+
vendor/bin/jobqueue run default \
47+
--config app/config/jobqueue.php \
48+
--executors-config app/config/executors.php \
49+
--log-file var/log/workers.log \
50+
--user jobserver \
51+
--host tarantool \
52+
'
53+
```
54+
55+
56+
## Tarantool
57+
58+
[Admin Web Interface](http://localhost:8001/)
59+
60+
To get into Tarantool console as admin on a running Docker container, run:
61+
62+
```bash
63+
docker-compose exec tarantool tarantoolctl connect /var/run/tarantool/tarantool.sock
64+
```
65+
66+
On a server:
67+
68+
```bash
69+
sudo tarantoolctl enter jobserver_instance
70+
```
71+
72+
On the server as a job queue user:
73+
74+
```bash
75+
sudo tarantoolctl connect $TNT_JOBQUEUE_USER:$TNT_JOBQUEUE_PASSWORD@$TNT_JOBQUEUE_HOST:3301
76+
```
77+
78+
79+
## Run tests
80+
81+
```bash
82+
docker-compose exec worker vendor/bin/phpunit
83+
```
84+
85+
86+
## License
87+
88+
The library is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.

app/app.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Di\Container;
6+
7+
function container(): Container
8+
{
9+
$getSettings = require __DIR__.'/config/settings.php';
10+
$container = new Container($getSettings(dirname(__DIR__)));
11+
12+
$container->setEnv(getenv('APP_ENV') ?: 'prod');
13+
$container->setDebug(getenv('APP_DEBUG') ?: false);
14+
15+
$localConfigFile = __DIR__.'/config/local.php';
16+
if (file_exists($localConfigFile)) {
17+
$extendContainer = require $localConfigFile;
18+
$container = $extendContainer($container);
19+
}
20+
21+
return $container;
22+
}

app/config/commands.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Di\Container;
6+
use App\UseCase\Greet\GreetCommand;
7+
8+
return function (Container $container): array {
9+
return [
10+
new GreetCommand($container->getGreetHandler()),
11+
];
12+
};

app/config/executors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return App\container()->getJobQueueExecutor();

app/config/jobqueue.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return App\container()->getJobQueueConfigFactory();

app/config/jobserver_config.lua.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local os = require('os')
2+
3+
return {
4+
user = os.getenv('TARANTOOL_USER_NAME'),
5+
password = os.getenv('TARANTOOL_USER_PASSWORD')
6+
}

app/config/jobserver_instance.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env tarantool
2+
3+
box.cfg {
4+
listen = 3301,
5+
log_level = 5
6+
}
7+
8+
if jobserver ~= nil then
9+
-- hot code reload using tarantoolctl or dofile()
10+
11+
-- unload old application
12+
jobserver.stop()
13+
-- clear cache for loaded modules and dependencies
14+
package.loaded['jobserver'] = nil
15+
end
16+
17+
local config = require('jobserver_config')
18+
19+
-- ensure a user exists
20+
if not box.schema.user.exists(config.user) then
21+
box.schema.user.create(config.user, {password = config.password})
22+
box.schema.user.grant(config.user, 'read,write,execute', 'universe', nil)
23+
end
24+
25+
-- load a new version of app and all dependencies
26+
jobserver = require('jobserver')
27+
jobserver.start(config)

app/config/local.php.dist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use App\Di\Container;
4+
use App\Di\Options;
5+
6+
return function (Container $container): Container {
7+
return $container
8+
->setEnv('dev')
9+
->setDebug(true)
10+
->set(Options::GREET_YELL, true)
11+
;
12+
};

app/config/settings.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use App\Di\Options;
4+
5+
return function (string $rootDir): array {
6+
return [
7+
Options::GREET_YELL => false,
8+
Options::LOGGER_FILE => "$rootDir/var/log/workers.log",
9+
];
10+
};

app/jobserver.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
queue = require('queue')
2+
3+
local function start(config)
4+
box.once('jobserver:v1.0', function()
5+
local tube = queue.create_tube('default', 'fifottl', {if_not_exists = true})
6+
-- temporary disabled as seems like this method is not yet released
7+
-- tube:grant(config.user)
8+
9+
-- tube:put({recurrence = 60, payload = {service = 'greet', args = {name = 'Foobar'}}}, {ttr = 300})
10+
end)
11+
end
12+
13+
local function stop()
14+
end
15+
16+
return {
17+
start = start,
18+
stop = stop
19+
}

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "tarantool/jobserver",
3+
"description": "A skeleton for jobqueue applications.",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Eugene Leonovich",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.1",
14+
"rybakit/arguments-resolver": "^0.5.1",
15+
"symfony/console": "^3.3",
16+
"tarantool/jobqueue": "dev-listener"
17+
},
18+
"require-dev": {
19+
"friendsofphp/php-cs-fixer": "^2.2",
20+
"phpunit/phpunit": "^6.1"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"App\\": "src/"
25+
},
26+
"files": [
27+
"app/app.php"
28+
]
29+
},
30+
"autoload-dev" : {
31+
"psr-4": {
32+
"App\\Tests\\": "tests/"
33+
}
34+
},
35+
"config": {
36+
"platform": {
37+
"php": "7.1"
38+
},
39+
"sort-packages": true
40+
}
41+
}

0 commit comments

Comments
 (0)