4
4
5
5
use Illuminate \Events \CallQueuedListener ;
6
6
use Illuminate \Queue \Events \JobQueued ;
7
+ use Illuminate \Queue \Events \JobQueueing ;
7
8
use Laravel \Nightwatch \Clock ;
8
9
use Laravel \Nightwatch \Concerns \NormalizesQueue ;
9
10
use Laravel \Nightwatch \Records \QueuedJob ;
10
11
use Laravel \Nightwatch \State \CommandState ;
11
12
use Laravel \Nightwatch \State \RequestState ;
12
13
use ReflectionClass ;
14
+ use RuntimeException ;
13
15
14
16
use function hash ;
15
17
use function is_object ;
16
18
use function is_string ;
17
19
use function method_exists ;
18
20
use function property_exists ;
21
+ use function round ;
19
22
20
23
/**
21
24
* @internal
@@ -24,6 +27,8 @@ final class QueuedJobSensor
24
27
{
25
28
use NormalizesQueue;
26
29
30
+ private ?float $ startTime = null ;
31
+
27
32
/**
28
33
* @param array<string, array{ queue?: string, driver?: string, prefix?: string, suffix?: string }> $connectionConfig
29
34
*/
@@ -35,19 +40,30 @@ public function __construct(
35
40
//
36
41
}
37
42
38
- public function __invoke (JobQueued $ event ): void
43
+ public function __invoke (JobQueueing | JobQueued $ event ): void
39
44
{
40
- $ nowMicrotime = $ this ->clock ->microtime ();
45
+ $ now = $ this ->clock ->microtime ();
46
+
47
+ if ($ event instanceof JobQueueing) {
48
+ $ this ->startTime = $ now ;
49
+
50
+ return ;
51
+ }
52
+
41
53
$ name = match (true ) {
42
54
is_string ($ event ->job ) => $ event ->job ,
43
55
method_exists ($ event ->job , 'displayName ' ) => $ event ->job ->displayName (),
44
56
default => $ event ->job ::class,
45
57
};
46
58
59
+ if ($ this ->startTime === null ) {
60
+ throw new RuntimeException ("No start time found for [ {$ name }]. " );
61
+ }
62
+
47
63
$ this ->executionState ->jobsQueued ++;
48
64
49
65
$ this ->executionState ->records ->write (new QueuedJob (
50
- timestamp: $ nowMicrotime ,
66
+ timestamp: $ now ,
51
67
deploy: $ this ->executionState ->deploy ,
52
68
server: $ this ->executionState ->server ,
53
69
_group: hash ('md5 ' , $ name ),
@@ -60,7 +76,7 @@ public function __invoke(JobQueued $event): void
60
76
name: $ name ,
61
77
connection: $ event ->connectionName ,
62
78
queue: $ this ->normalizeQueue ($ event ->connectionName , $ this ->resolveQueue ($ event )),
63
- duration: 0 , // TODO
79
+ duration: ( int ) round (( $ now - $ this -> startTime ) * 1_000_000 )
64
80
));
65
81
}
66
82
0 commit comments