trypost/app/Jobs/Automation/ProcessAutomationNode.php
Paulo Castellano 4efaa0bf99 Harden automations module: full-post generation, reliable runs, editor UX
Generation
- Generate node now produces the full post (text + AI image + carousel)
  via a shared PostImagePipeline extracted from StreamPostCreation
- Generate config UI mirrors the /posts/create wizard (carousel slide
  count, include-image toggle); drop the decorative format/unsplash keys

Flow correctness
- RSS/HTTP nodes expose named has-items (default) and no-items output
  handles, labeled and colored like the Condition node
- AdvanceAutomationRun records a no_matching_edge terminal instead of
  completing silently; "0 new items" feedback in the test panel
- Manual/test runs no longer persist the production dedup watermark

Run reliability
- Pause truly halts in-flight runs (production only; manual test runs
  always run regardless of automation status)
- ProcessAutomationNode::failed() marks the run failed
- automation:recover-stuck-runs and automation:prune-dry-runs commands

Webhook / HTTP
- Branded User-Agent (config-driven) on outbound webhook + http_request
- Webhook fails on invalid JSON instead of silently sending {}
- HTTP custom headers editor; CodeMirror-based CodeEditor for JSON

Editor UX
- Header Test button only opens the panel; the panel has a Run button
  (saves first) and owns the with-real-data toggle
- Clicking a node closes the test panel and opens its config
- Node cards: max-width + truncate so long URLs don't grow the node
2026-06-10 20:45:01 -03:00

155 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs\Automation;
use App\Actions\Automation\Node\RunConditionNode;
use App\Actions\Automation\Node\RunDelayNode;
use App\Actions\Automation\Node\RunEndNode;
use App\Actions\Automation\Node\RunFetchRssNode;
use App\Actions\Automation\Node\RunGenerateNode;
use App\Actions\Automation\Node\RunHttpRequestNode;
use App\Actions\Automation\Node\RunPublishNode;
use App\Actions\Automation\Node\RunWebhookNode;
use App\Actions\Automation\Run\AdvanceAutomationRun;
use App\DataTransferObjects\Automation\NodeRunResult;
use App\Enums\Automation\Node\Type as NodeType;
use App\Enums\Automation\NodeRun\Status as NodeRunStatus;
use App\Enums\Automation\Run\Status as RunStatus;
use App\Enums\Automation\Status as AutomationStatus;
use App\Models\AutomationNodeRun;
use App\Models\AutomationRun;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use LogicException;
use Throwable;
class ProcessAutomationNode implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 1;
public function __construct(
public AutomationRun $run,
public string $nodeId,
) {
$this->onQueue('automations');
}
public function handle(AdvanceAutomationRun $advance): void
{
$this->run->refresh();
if (! in_array($this->run->status, [RunStatus::Pending, RunStatus::Running, RunStatus::Waiting], true)) {
return;
}
if (! $this->run->is_manual && $this->run->automation->status !== AutomationStatus::Active) {
return;
}
$node = collect($this->run->automation->nodes ?? [])->firstWhere('id', $this->nodeId);
if ($node === null) {
$this->run->update([
'status' => RunStatus::Failed,
'error' => ['message' => __('automations.errors.node_no_longer_exists', ['node_id' => $this->nodeId])],
'finished_at' => now(),
]);
return;
}
$nodeType = NodeType::from($node['type']);
$this->run->update([
'status' => RunStatus::Running,
'current_node_id' => $this->nodeId,
'started_at' => $this->run->started_at ?? now(),
]);
$nodeRun = AutomationNodeRun::create([
'run_id' => $this->run->id,
'node_id' => $this->nodeId,
'node_type' => $nodeType,
'status' => NodeRunStatus::Running,
'input' => $this->run->context,
'started_at' => now(),
]);
try {
$result = $this->executeNode($nodeType, $node['data'] ?? []);
} catch (Throwable $e) {
$result = NodeRunResult::failed($e->getMessage(), ['class' => $e::class]);
}
$nodeRun->update([
'status' => $result->status,
'output' => $result->output,
'error' => $result->error,
'finished_at' => now(),
]);
if ($result->status === NodeRunStatus::Failed) {
$this->run->update([
'status' => RunStatus::Failed,
'error' => array_merge(['node_id' => $this->nodeId], $result->error ?? []),
'finished_at' => now(),
]);
return;
}
$this->run->update([
'context' => array_merge($this->run->context ?? [], $result->output),
]);
if ($result->sleepUntil !== null) {
$this->run->update([
'status' => RunStatus::Waiting,
'next_action_at' => $result->sleepUntil,
]);
return;
}
$advance($this->run, $this->nodeId, $result->nextHandle);
}
public function failed(?Throwable $e): void
{
$this->run->refresh();
if (in_array($this->run->status, [RunStatus::Completed, RunStatus::Failed], true)) {
return;
}
$this->run->update([
'status' => RunStatus::Failed,
'error' => ['message' => $e?->getMessage() ?? 'job failed', 'node_id' => $this->nodeId],
'finished_at' => now(),
]);
}
private function executeNode(NodeType $type, array $config): NodeRunResult
{
$handler = match ($type) {
NodeType::Generate => app(RunGenerateNode::class),
NodeType::Delay => app(RunDelayNode::class),
NodeType::Condition => app(RunConditionNode::class),
NodeType::Publish => app(RunPublishNode::class),
NodeType::Webhook => app(RunWebhookNode::class),
NodeType::End => app(RunEndNode::class),
NodeType::FetchRss => app(RunFetchRssNode::class),
NodeType::HttpRequest => app(RunHttpRequestNode::class),
NodeType::Trigger => throw new LogicException('Trigger nodes are not executed as run steps.'),
};
return $handler($this->run, $config);
}
}