@@ -20,18 +20,17 @@ public function __invoke(): JsonResponse
20
20
21
21
$ response = [];
22
22
23
+ $ logContext = [];
24
+
23
25
// Check database connection
24
26
if (config ('healthcheck.database ' , true ) === true ) {
25
27
try {
26
28
DB ::connection ()->getPdo ();
27
29
$ dbStatus = 'OK ' ;
30
+ $ logContext ['is_database_connected ' ] = true ;
28
31
} catch (Exception $ e ) {
29
32
$ status = 500 ;
30
- // Add logs for cloudwatch to monitor
31
- Log::error ('Unable to connect to Database ' , [
32
- 'is_database_connected ' => false ,
33
- 'include_in_metrics ' => false ,
34
- ]);
33
+ $ logContext ['is_database_connected ' ] = false ;
35
34
$ dbStatus = 'Unable to connect to the database: ' . $ e ->getMessage ();
36
35
}
37
36
$ response ['db_status ' ] = $ dbStatus ;
@@ -42,15 +41,11 @@ public function __invoke(): JsonResponse
42
41
try {
43
42
Redis::ping ();
44
43
$ redisStatus = 'OK ' ;
44
+ $ logContext ['is_redis_connected ' ] = true ;
45
45
} catch (Exception $ e ) {
46
46
$ status = 500 ;
47
47
$ redisStatus = 'Unable to connect to Redis: ' . $ e ->getMessage ();
48
-
49
- // Add logs for cloudwatch to monitor
50
- Log::error ('Unable to connect to Redis ' , [
51
- 'is_redis_connected ' => false ,
52
- 'include_in_metrics ' => false ,
53
- ]);
48
+ $ logContext ['is_redis_connected ' ] = false ;
54
49
}
55
50
$ response ['redis_status ' ] = $ redisStatus ;
56
51
}
@@ -63,33 +58,33 @@ public function __invoke(): JsonResponse
63
58
$ horizonStatus = 'Horizon is currently ' . $ horizonCurrentStatus ;
64
59
if ($ horizonCurrentStatus === 'running ' ) {
65
60
$ horizonStatus = 'OK ' ;
61
+ $ logContext ['is_horizon_running ' ] = true ;
66
62
}
67
63
}
68
64
if ($ horizonStatus !== 'OK ' ) {
69
65
// Add logs for cloudwatch to monitor
70
- Log::error ($ horizonStatus , [
71
- 'is_horizon_running ' => false ,
72
- 'include_in_metrics ' => false ,
73
- ]);
66
+ $ logContext ['is_horizon_running ' ] = false ;
74
67
}
75
68
$ response ['horizon_status ' ] = $ horizonStatus ;
76
69
}
77
70
78
71
if (config ('healthcheck.scheduler ' , false )) {
79
72
$ response ['scheduler_status ' ] = 'OK ' ;
80
73
$ response ['scheduler_last_run ' ] = $ this ->getLastRun ()?->format('Y-m-d H:i:s ' );
74
+ $ logContext ['is_scheduler_running ' ] = true ;
75
+ $ logContext ['scheduler_last_run ' ] = $ response ['scheduler_last_run ' ];
81
76
82
77
// Check if the last run was more than or equal to 2 minutes ago
83
78
if (empty ($ this ->getLastRun ()) || $ this ->getLastRun ()?->diffInMinutes(Carbon::now ()) >= 2 ) {
84
79
$ response ['scheduler_status ' ] = 'Scheduler is not running ' ;
85
- Log::error ('Scheduler is not running. ' , [
86
- 'is_scheduler_running ' => false ,
87
- 'last_run ' => $ this ->getLastRun ()?->format('Y-m-d H:i:s ' ),
88
- 'include_in_metrics ' => false ,
89
- ]);
80
+ $ logContext ['is_scheduler_running ' ] = false ;
90
81
}
91
82
}
92
83
84
+ if (config ('healthcheck.logging ' , true )) {
85
+ Log::info ('Healthcheck status ' , $ logContext );
86
+ }
87
+
93
88
// Assign status to the response but put it in the beginning of the array
94
89
$ response = array_merge (['status ' => $ status ], $ response );
95
90
0 commit comments