start()

in app/routes/api.jobs.$jobId.logs.tsx [25:120]


    start(controller) {
      const encoder = new TextEncoder();

      // Send initial connection message
      controller.enqueue(
        encoder.encode(
          `data: ${JSON.stringify({ type: "connected", jobId })}\n\n`
        )
      );

      let intervalId: NodeJS.Timeout;
      let lastLogLength = 0;

      const sendLogs = async () => {
        try {
          const jobStore = getJobStore();
          const job = await jobStore.getJob(jobId);

          if (!job) {
            controller.enqueue(
              encoder.encode(
                `data: ${JSON.stringify({ type: "error", message: "Job not found" })}\n\n`
              )
            );
            return;
          }

          // Get current logs
          const logs = await jobStore.getJobLogs(jobId);

          if (logs && logs.length > lastLogLength) {
            // Send only new log content
            const newLogs = logs.substring(lastLogLength);
            lastLogLength = logs.length;

            controller.enqueue(
              encoder.encode(
                `data: ${JSON.stringify({
                  type: "logs",
                  data: newLogs,
                  timestamp: new Date().toISOString(),
                })}\n\n`
              )
            );
          }

          // Send job status update
          controller.enqueue(
            encoder.encode(
              `data: ${JSON.stringify({
                type: "status",
                status: job.status,
                timestamp: new Date().toISOString(),
              })}\n\n`
            )
          );

          // If job is completed or failed, stop streaming
          if (job.status === "completed" || job.status === "failed") {
            controller.enqueue(
              encoder.encode(
                `data: ${JSON.stringify({
                  type: "finished",
                  status: job.status,
                  timestamp: new Date().toISOString(),
                })}\n\n`
              )
            );
            clearInterval(intervalId);
            controller.close();
          }
        } catch (error) {
          console.error("Error streaming logs:", error);
          controller.enqueue(
            encoder.encode(
              `data: ${JSON.stringify({
                type: "error",
                message: "Failed to fetch logs",
              })}\n\n`
            )
          );
        }
      };

      // Start streaming logs every 2 seconds
      intervalId = setInterval(sendLogs, 2000);

      // Send initial logs immediately
      sendLogs();

      // Clean up on client disconnect
      request.signal.addEventListener("abort", () => {
        clearInterval(intervalId);
        controller.close();
      });
    },