Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
StopWorkerOnMemoryLimitListener example
$stopsWhen
[
]
= "processed {
$limit
}
messages";
$this
->eventDispatcher->
addSubscriber
(
new
StopWorkerOnMessageLimitListener
(
$limit
,
$this
->logger
)
)
;
}
if
(
$failureLimit
=
$input
->
getOption
(
'failure-limit'
)
)
{
$stopsWhen
[
]
= "reached {
$failureLimit
}
failed messages";
$this
->eventDispatcher->
addSubscriber
(
new
StopWorkerOnFailureLimitListener
(
$failureLimit
,
$this
->logger
)
)
;
}
if
(
$memoryLimit
=
$input
->
getOption
(
'memory-limit'
)
)
{
$stopsWhen
[
]
= "exceeded {
$memoryLimit
}
of memory";
$this
->eventDispatcher->
addSubscriber
(
new
StopWorkerOnMemoryLimitListener
(
$this
->
convertToBytes
(
$memoryLimit
)
,
$this
->logger
)
)
;
}
if
(
null !==
$timeLimit
=
$input
->
getOption
(
'time-limit'
)
)
{
if
(
!
is_numeric
(
$timeLimit
)
|| 0 >=
$timeLimit
)
{
throw
new
InvalidOptionException
(
sprintf
(
'Option "time-limit" must be a positive integer, "%s" passed.',
$timeLimit
)
)
;
}
$stopsWhen
[
]
= "been running for {
$timeLimit
}
s";
$this
->eventDispatcher->
addSubscriber
(
new
StopWorkerOnTimeLimitListener
(
$timeLimit
,
$this
->logger
)
)
;
}
/** * @dataProvider memoryProvider */
public
function
testWorkerStopsWhenMemoryLimitExceeded
(
int
$memoryUsage
, int
$memoryLimit
, bool
$shouldStop
)
{
$memoryResolver
=
fn
(
)
=>
$memoryUsage
;
$worker
=
$this
->
createMock
(
Worker::
class
)
;
$worker
->
expects
(
$shouldStop
?
$this
->
once
(
)
:
$this
->
never
(
)
)
->
method
(
'stop'
)
;
$event
=
new
WorkerRunningEvent
(
$worker
, false
)
;
$memoryLimitListener
=
new
StopWorkerOnMemoryLimitListener
(
$memoryLimit
, null,
$memoryResolver
)
;
$memoryLimitListener
->
onWorkerRunning
(
$event
)
;
}
public
static
function
memoryProvider
(
)
: iterable
{
yield
[
2048, 1024, true
]
;
yield
[
1024, 1024, false
]
;
yield
[
1024, 2048, false
]
;
}
public
function
testWorkerLogsMemoryExceededWhenLoggerIsGiven
(
)
{