Skip to content

Add error handling and retry logic to GenerateAltTextJob #56

@robocopklaus

Description

@robocopklaus

Summary

GenerateAltTextJob has no error handling, retry configuration, or timeout. A transient OpenAI API failure permanently loses the job, and partial results from a multi-locale loop are never saved.

Problem

No retry configuration

// GenerateAltTextJob.php — missing properties
// No $tries, $backoff, $timeout defined

Laravel defaults to $tries = 1 (no retries). A transient API error (429 rate limit, 500 server error, network timeout) permanently fails the job.

No try/catch in generation loop

// GenerateAltTextJob.php:44-55
foreach ($this->fieldMapping as $fieldName => $locale) {
    $result = $generator->generate($asset, $locale);  // If this throws on locale 2 of 3...
    $asset->set($fieldName, trim($result));             // ...locale 1's set() is lost
}
$asset->save();  // ...never reached

If the generator throws on the 2nd locale of a 3-locale setup, the 1st locale's set() was called but save() is never reached. All work is lost.

TODO: incomplete response not handled

The OpenAIGenerator.php has a TODO comment acknowledging that finishReason is not checked. An incomplete or content-filtered response may produce truncated or empty alt text.

Suggested Fix

class GenerateAltTextJob implements ShouldQueue
{
    public $tries = 3;
    public $backoff = [10, 30, 60];
    public $timeout = 120;

    public function handle(Generator $generator): void
    {
        $asset = Asset::find($this->assetId);
        // ...
        foreach ($this->fieldMapping as $fieldName => $locale) {
            try {
                $result = $generator->generate($asset, $locale);
                if (!empty(trim($result))) {
                    $asset->set($fieldName, trim($result));
                }
            } catch (\Throwable $e) {
                report($e);
                // Continue with other locales
            }
        }
        $asset->save();
    }
}

References

  • File: src/Jobs/GenerateAltTextJob.php:44-55
  • File: src/Generator/OpenAIGenerator.php:55-58
  • Consolidated review findings: V4, B1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthighHigh severity finding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions