Summer Sale! All accounts are 50% off this week.

abkrim's avatar
Level 13

Exceptions in Job. Too much information that I cannot or will not limit myself

I have tried to limit the exception reporting in work queues.

Example

The image working, but I hit an error in CallAnalyzerAction

if ($response->clientError()) {
     //ray('Client error');
     throw new SlimExceptions("SLIM client error {$response->status()}",
         $response->status(), $response->json());
}

Horizon

In the log, I see it well.

[2023-12-11 10:18:24] production.ERROR: [LOGGER][ANALYZER] call #{"id":3964,"name":"Cerdido-CM20","short_name":null,"imei":"861774050456134","sim":null,"address":"Aldea Carballal 0016","ip":null,"id_location":null,"lat":"43.608726","lon":"-8.011915","astro_clock":false,"scene":null,"manual":null,"is_astro_php":1,"sunrise_offset":null,"sunrise":"2023-12-06 07:49:07","sunrise_fallback":"2023-12-06 07:49:07","sunset_offset":null,"sunset":"2023-12-06 16:57:21","sunset_fallback":"2023-12-06 16:57:21","initialized_at":"2023-06-21 07:26:30","initialized_lums_at":null,"last_update":null,"manufacturer_id":1,"patron":null,"counter_plc":0,"town_hall_id":27,"status":0,"in_maintenance":0,"created_at":null,"updated_at":"2023-12-06 16:32:27","dl":null,"switch":null,"trafos":null,"power_gis":null,"voltage":null,"blocked":null,"checked_at":"2023-12-06 16:32:27","hw_version":null,"municipality":"Cerdido","friendly_name":null,"counter":null,"cups":"ES0022000008445699QV1P","last_streetlight_id":null,"max_power":null,"min_power":null,"observations":null,"external_charges":null,"external_changes":null,"mac":null,"geometry":null,"alerts":0,"uptime":null,"manufacturer":{"id":1,"name":"Solar Power","endpoint":"SolarPower","get_events_modem":0,"datos":null,"created_at":"2022-04-13 07:51:59","updated_at":"2022-04-13 07:51:59"}}->id ${"id":3964,"name":"Cerdido-CM20","short_name":null,"imei":"861774050456134","sim":null,"address":"Aldea Carballal 0016","ip":null,"id_location":null,"lat":"43.608726","lon":"-8.011915","astro_clock":false,"scene":null,"manual":null,"is_astro_php":1,"sunrise_offset":null,"sunrise":"2023-12-06 07:49:07","sunrise_fallback":"2023-12-06 07:49:07","sunset_offset":null,"sunset":"2023-12-06 16:57:21","sunset_fallback":"2023-12-06 16:57:21","initialized_at":"2023-06-21 07:26:30","initialized_lums_at":null,"last_update":null,"manufacturer_id":1,"patron":null,"counter_plc":0,"town_hall_id":27,"status":0,"in_maintenance":0,"created_at":null,"updated_at":"2023-12-06 16:32:27","dl":null,"switch":null,"trafos":null,"power_gis":null,"voltage":null,"blocked":null,"checked_at":"2023-12-06 16:32:27","hw_version":null,"municipality":"Cerdido","friendly_name":null,"counter":null,"cups":"ES0022000008445699QV1P","last_streetlight_id":null,"max_power":null,"min_power":null,"observations":null,"external_charges":null,"external_changes":null,"mac":null,"geometry":null,"alerts":0,"uptime":null,"manufacturer":{"id":1,"name":"Solar Power","endpoint":"SolarPower","get_events_modem":0,"datos":null,"created_at":"2022-04-13 07:51:59","updated_at":"2022-04-13 07:51:59"}}->name 2023-12-11 10:15:12 SLIM client error 400 ERROR: Modem no operativo [2023-12-11 10:18:25] production.ERROR: [LOGGER][ANALYZER] call #4569 Camargo-CM702 2023-12-11 10:15:12 No captured exception Attempts: 2 SLIM client error 400 ERROR: Modem no operativo [2023-12-11 10:18:25] production.ERROR: [LOGGER][ANALYZER] call #{"id":4569,"name":"Camargo-CM702","short_name":null,"imei":"861774050506961","sim":null,"address":null,"ip":null,"id_location":null,"lat":"43.420318","lon":"-3.841630","astro_clock":false,"scene":null,"manual":null,"is_astro_php":1,"sunrise_offset":null,"sunrise":"2023-12-06 07:31:49","sunrise_fallback":"2023-12-06 07:31:49","sunset_offset":null,"sunset":"2023-12-06 16:41:18","sunset_fallback":"2023-12-06 16:41:18","initialized_at":"2023-03-28 12:15:11","initialized_lums_at":null,"last_update":null,"manufacturer_id":1,"patron":null,"counter_plc":0,"town_hall_id":2,"status":0,"in_maintenance":0,"created_at":null,"updated_at":"2023-12-06 16:17:41","dl":null,"switch":null,"trafos":null,"power_gis":null,"voltage":null,"blocked":null,"checked_at":"2023-12-06 16:17:41","hw_version":null,"municipality":null,"friendly_name":null,"counter":null,"cups":null,"last_streetlight_id":null,"max_power":null,"min_power":null,"observations":null,"external_charges":null,"external_changes":null,"mac":null,"geometry":null,"alerts":0,"uptime":null,"manufacturer":{"id":1,"name":"Solar Power","endpoint":"SolarPower","get_events_modem":0,"datos":null,"created_at":"2022-04-13 07:51:59","updated_at":"2022-04-13 07:51:59"}}->id ${"id":4569,"name":"Camargo-CM702","short_name":null,"imei":"861774050506961","sim":null,"address":null,"ip":null,"id_location":null,"lat":"43.420318","lon":"-3.841630","astro_clock":false,"scene":null,"manual":null,"is_astro_php":1,"sunrise_offset":null,"sunrise":"2023-12-06 07:31:49","sunrise_fallback":"2023-12-06 07:31:49","sunset_offset":null,"sunset":"2023-12-06 16:41:18","sunset_fallback":"2023-12-06 16:41:18","initialized_at":"2023-03-28 12:15:11","initialized_lums_at":null,"last_update":null,"manufacturer_id":1,"patron":null,"counter_plc":0,"town_hall_id":2,"status":0,"in_maintenance":0,"created_at":null,"updated_at":"2023-12-06 16:17:41","dl":null,"switch":null,"trafos":null,"power_gis":null,"voltage":null,"blocked":null,"checked_at":"2023-12-06 16:17:41","hw_version":null,"municipality":null,"friendly_name":null,"counter":null,"cups":null,"last_streetlight_id":null,"max_power":null,"min_power":null,"observations":null,"external_charges":null,"external_changes":null,"mac":null,"geometry":null,"alerts":0,"uptime":null,"manufacturer":{"id":1,"name":"Solar Power","endpoint":"SolarPower","get_events_modem":0,"datos":null,"created_at":"2022-04-13 07:51:59","updated_at":"2022-04-13 07:51:59"}}->name 2023-12-11 10:15:12 SLIM client error 400 ERROR: Modem no operativo


## Job

```php
try {
    CallAnalyzerAction::run(
        $this->commandCenter,
        $this->job_time,
        $this->initial_delayed_at,
        $this->debug,
        $this->is_multiple,
        false,
        $this->tries,
        $this->attempts(),
        $this->key_multiple
    );

} catch (Exception $e) {
    $message = "{$this->message} No captured exception Attempts: {$this->attempts()} {$e->getMessage()}";
    Log::error($message);
    if (($this->connection == 'sync') || ($this->attempts() >= $this->tries)) {
        $this->sendToElasticsearchFromJob(500, $message);
        $this->fail($e->getMessage());
    } else {
        $this->release();
    }
}



public function failed(Exception $exception): void
{
    $message = "[LOGGER][ANALYZER] call #$this->commandCenter->id $$this->commandCenter->name $this->job_time "
        . $exception->getMessage();
    $this->sendToElasticsearchFromJob(429, $exception->getMessage());
    Log::error($message);
}

protected function sendToElasticsearchFromJob(int $statusCode, string $message_log): void
{
    // First update LoggerAnalyzer
    Log::debug("Send to Elasticsearch Failed Job for {$this->commandCenter->id} {$this->job_time} $message_log");

    $logger = LoggerAnalyzer::updateOrCreate(
        [
            'command_center_id' => $this->commandCenter->id,
            'job' => $this->job_time,
            'delayed_at' => $this->initial_delayed_at,
        ],
        [
            'status_code' => $statusCode,
            'data' => json_encode([
                'data' => null,
                'message' => $message_log,
                'success' => false,
            ]),
        ]
    );

    $elastic_id = $this->sendToElasticsearch($logger->id, 'analyzer');

    // Update elastic_id if return OK from Elasticsearch
    if (! is_null($elastic_id)) {
        $logger->elastic_id = $elastic_id;
        $logger->update();
    }
}
0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're trying to limit the exception reporting in your job queue, but you're encountering issues with too much information being logged. To address this, you can customize the exception handling within your job to only log the necessary information.

Here's a revised version of your job's catch block and failed method that limits the information being logged:

try {
    // Your existing code to call the action...
} catch (Exception $e) {
    $this->handleException($e);
}

protected function handleException(Exception $exception): void
{
    $message = "Error in job {$this->job->getJobId()} for command center ID {$this->commandCenter->id}: {$exception->getMessage()}";
    Log::error($message);

    if ($this->shouldReportException($exception)) {
        $this->reportException($exception);
    }

    if ($this->shouldFailJob($exception)) {
        $this->fail($exception);
    } else {
        $this->release();
    }
}

protected function shouldReportException(Exception $exception): bool
{
    // Add logic here to determine if the exception should be reported
    // For example, you might want to report only certain types of exceptions
    // or exceptions that occur after a certain number of attempts
    return true;
}

protected function reportException(Exception $exception): void
{
    // Your existing code to report the exception...
}

protected function shouldFailJob(Exception $exception): bool
{
    // Determine if the job should be marked as failed
    // For example, you might want to fail the job only after a certain number of attempts
    return $this->attempts() >= $this->tries;
}

public function failed(Exception $exception): void
{
    // Simplify the message to include only the necessary information
    $message = "Job failed for command center ID {$this->commandCenter->id}: {$exception->getMessage()}";
    Log::error($message);
    // Your existing code to handle a failed job...
}

In this solution, I've added helper methods handleException, shouldReportException, reportException, and shouldFailJob to encapsulate the logic for handling exceptions. This allows you to easily adjust the conditions under which exceptions are reported and jobs are failed.

Make sure to replace the placeholders and logic with the actual conditions and information relevant to your application. The goal is to log only the essential information and to avoid logging sensitive data or overly verbose error messages.

martinbean's avatar

@abkrim Can you share a screenshot via an image service that isn’t full of wholly inappropriate adverts, please?

abkrim's avatar
Level 13

@martinbean I'm ashamed. I never imagined that this service would have to go down, despite having indicated Family Content.

From now on I will use my multimedia server.

martinbean's avatar

@abkrim Third-party image services are fine. Just might be worthwhile to stick to something like imgur in the future 🙂

Please or to participate in this conversation.