Skip to content

Commit 057c3e6

Browse files
committed
allow to stop timing the current transaction before starting processing tasks in background
1 parent 547167a commit 057c3e6

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/CrowdStar/BackgroundProcessing/BackgroundProcessing.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace CrowdStar\BackgroundProcessing;
1919

2020
use Closure;
21+
use CrowdStar\BackgroundProcessing\Timer\AbstractTimer;
2122

2223
/**
2324
* Class BackgroundProcessing
@@ -27,20 +28,26 @@
2728
class BackgroundProcessing
2829
{
2930
/**
30-
* @var array
31+
* @var Closure[]
3132
*/
3233
protected static $closures = [];
3334

35+
/**
36+
* @var AbstractTimer[]
37+
*/
38+
protected static $timers = [];
39+
3440
/**
3541
* @var bool
3642
*/
3743
protected static $invoked = false;
3844

3945
/**
46+
* @param bool $stopTiming Stop timing the current transaction or not before starting processing tasks in background
4047
* @return void
4148
* @throws Exception
4249
*/
43-
public static function run()
50+
public static function run(bool $stopTiming = false)
4451
{
4552
if (self::isInvoked()) {
4653
throw new Exception('background process invoked already');
@@ -53,6 +60,13 @@ public static function run()
5360
session_write_close();
5461
fastcgi_finish_request();
5562

63+
if ($stopTiming) {
64+
// Stop timing the current transaction before starting processing tasks in background.
65+
foreach (self::$timers as $timer) {
66+
$timer->stopTiming();
67+
}
68+
}
69+
5670
foreach (self::$closures as $closure) {
5771
$closure();
5872
}
@@ -82,6 +96,14 @@ public static function add(Closure $op, ...$params)
8296
};
8397
}
8498

99+
/**
100+
* @param AbstractTimer $timer
101+
*/
102+
public static function addTimer(AbstractTimer $timer)
103+
{
104+
self::$timers[] = $timer;
105+
}
106+
85107
/**
86108
* @param bool $invoked
87109
* @return void
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**************************************************************************
3+
* Copyright 2018 Glu Mobile Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*************************************************************************/
17+
18+
namespace CrowdStar\BackgroundProcessing\Timer;
19+
20+
/**
21+
* Class AbstractTimer
22+
*
23+
* @package CrowdStar\BackgroundProcessing\Timer
24+
*/
25+
abstract class AbstractTimer
26+
{
27+
/**
28+
* Stop timing the current transaction before starting processing tasks in background.
29+
*
30+
* @return AbstractTimer
31+
*/
32+
abstract public function stopTiming(): AbstractTimer;
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**************************************************************************
3+
* Copyright 2018 Glu Mobile Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*************************************************************************/
17+
18+
namespace CrowdStar\BackgroundProcessing\Timer;
19+
20+
/**
21+
* Class NewRelicTimer
22+
*
23+
* @package CrowdStar\BackgroundProcessing\Timer
24+
*/
25+
class NewRelicTimer extends AbstractTimer
26+
{
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function stopTiming(): AbstractTimer
31+
{
32+
if (extension_loaded('newrelic')) {
33+
// Stop timing the current transaction in New Relic.
34+
newrelic_end_of_transaction();
35+
}
36+
37+
return $this;
38+
}
39+
}

0 commit comments

Comments
 (0)