Skip to content

Commit 3392c55

Browse files
authored
Merge pull request #5 from php-task/feature/lock
Added locking section
2 parents 6e17065 + f00cedf commit 3392c55

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Contents
3030
:maxdepth: 2
3131

3232
components
33+
locking
3334
quick-example
3435
symfony
3536

locking.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Locking
2+
=======
3+
To avoid concurrency issues, the library is able to lock other executions which
4+
would run at the same time. This could happen if multiple workers (e.g. cronjobs)
5+
run at the same time.
6+
7+
Each ``TaskHandler`` decides if and how many other Tasks will be blocked while a
8+
task with this handler is running. If the ``TaskHandler`` implements the
9+
``LockingTaskHandlerInterface`` the Lock-Component is enabled for this handler.
10+
The interface consists of a single method ``getLockKey($workload)`` which
11+
returns a locking-key. Tasks with the same locking-key will not be executed at
12+
the same time.
13+
14+
Example
15+
*******
16+
17+
.. code-block:: php
18+
19+
<?php
20+
21+
include __DIR__ . '/vendor/autoload.php';
22+
23+
class ImageResizeHandler implements Task\Lock\LockingTaskHandlerInterface
24+
{
25+
public function handle($workload)
26+
{
27+
...
28+
}
29+
30+
public function getLockKey($workload)
31+
{
32+
return self::class;
33+
34+
// or append workload data
35+
return self::class . '-' . $workload['id'];
36+
}
37+
}

symfony.rst

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Additional features which are implemented in this bundle.
1212
* Persist tasks and executions in database
1313
* Run statistics foreach execution of tasks
1414
* Predefined system-tasks
15+
* Locking mechanism to avoid concurrency problems
1516

1617
Installation
1718
------------
@@ -54,7 +55,6 @@ tasks.
5455

5556
System-Tasks
5657
------------
57-
5858
System-tasks can be used to predefine tasks for deployment. The developer
5959
can define which handler will be called (with an ``cron_expression`` and
6060
a ``workload``). This tasks can be scheduled with the following command.
@@ -79,23 +79,41 @@ supported.
7979
After addition or changing in the config you have to run the command again
8080
to be sure that the task-table will be updated.
8181

82+
Locking
83+
-------
84+
Locking is used to avoid concurrency problems when multiple task-runners run at
85+
the same time (see :doc:`locking`). This feature has to be enabled and will have
86+
multiple different storages in the future.
87+
88+
Currently only file storage is implemented and usable.
89+
8290
Configuration Reference
8391
-----------------------
8492

8593
.. code-block:: yaml
86-
8794
task:
88-
storage: doctrine # One of "array"; "doctrine"
95+
storage: doctrine # One of "array"; "doctrine"
8996
adapters:
9097
doctrine:
91-
clear: true
98+
clear: true
9299
run:
93100
mode: 'off' # One of "off"; "listener"
101+
locking:
102+
enabled: false
103+
storage: file # One of "file"
104+
ttl: 600
105+
storages:
106+
file:
107+
directory: '%kernel.cache_dir%/tasks'
94108
system_tasks:
95-
enabled: true
96-
handler_class: ~
97-
workload: null
98-
cron_expression: ~
109+
110+
# Prototype
111+
-
112+
enabled: true
113+
handler_class: ~
114+
workload: null
115+
cron_expression: ~
116+
99117
100118
.. _fastcgi_finish_request: http://php.net/manual/en/function.fastcgi-finish-request.php
101119
.. _PHP FPM: http://php.net/manual/en/install.fpm.php

0 commit comments

Comments
 (0)