Compare commits
3
Commits
7145fb536a
..
v1.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36c80458e0 | ||
|
|
06cf4d1b9e | ||
|
|
4f1d306fdf |
@@ -442,6 +442,25 @@ resource "coder_agent" "main" {
|
||||
web_terminal = true
|
||||
}
|
||||
|
||||
metadata {
|
||||
display_name = "Task Status"
|
||||
key = "task_status"
|
||||
script = <<-EOS
|
||||
if [ -f ~/task-output.log ]; then
|
||||
if grep -q "Task completed" ~/task-output.log 2>/dev/null; then
|
||||
echo "✅ Complete"
|
||||
else
|
||||
echo "⏳ Running ($(wc -l < ~/task-output.log) lines)"
|
||||
fi
|
||||
elif [ -n "$TASK_TYPE" ]; then
|
||||
echo "⏳ Starting..."
|
||||
else
|
||||
echo "Interactive"
|
||||
fi
|
||||
EOS
|
||||
interval = 10
|
||||
}
|
||||
|
||||
env = {
|
||||
ANTHROPIC_API_KEY = data.coder_parameter.anthropic_api_key.value
|
||||
GITHUB_TOKEN = data.coder_external_auth.github.access_token
|
||||
@@ -516,6 +535,41 @@ resource "coder_agent" "main" {
|
||||
npm run db:migrate:local
|
||||
fi
|
||||
|
||||
# --- Start log viewer on port 13338 ---
|
||||
if [ -n "$TASK_TYPE" ]; then
|
||||
touch ~/task-output.log
|
||||
cat > ~/log-server.sh << 'LOGEOF'
|
||||
#!/bin/bash
|
||||
while true; do
|
||||
{
|
||||
echo "HTTP/1.1 200 OK"
|
||||
echo "Content-Type: text/html; charset=utf-8"
|
||||
echo "Connection: close"
|
||||
echo ""
|
||||
echo "<html><head><title>Task Log</title>"
|
||||
echo "<meta http-equiv='refresh' content='5'>"
|
||||
echo "<style>body{background:#1e1e1e;color:#d4d4d4;font-family:monospace;font-size:13px;padding:16px;white-space:pre-wrap;}"
|
||||
echo "h2{color:#569cd6;margin:0 0 8px}.meta{color:#6a9955;margin-bottom:16px;display:block}</style></head><body>"
|
||||
echo "<h2>$TASK_TYPE #$ISSUE_NUMBER — $(hostname)</h2>"
|
||||
if [ -f ~/task-output.log ]; then
|
||||
LINES=$(wc -l < ~/task-output.log)
|
||||
if grep -q "Task completed" ~/task-output.log 2>/dev/null; then
|
||||
echo "<span class='meta'>Status: ✅ Complete ($LINES lines)</span>"
|
||||
else
|
||||
echo "<span class='meta'>Status: ⏳ Running ($LINES lines) — auto-refreshing every 5s</span>"
|
||||
fi
|
||||
sed 's/&/\&/g; s/</\</g; s/>/\>/g' ~/task-output.log
|
||||
else
|
||||
echo "Waiting for task to start..."
|
||||
fi
|
||||
echo "</body></html>"
|
||||
} | nc -l -p 13338 -q 1 2>/dev/null || true
|
||||
done
|
||||
LOGEOF
|
||||
chmod +x ~/log-server.sh
|
||||
nohup bash ~/log-server.sh &>/dev/null &
|
||||
fi
|
||||
|
||||
# --- Automated task execution ---
|
||||
if [ -n "$TASK_TYPE" ] && [ -d ~/project ]; then
|
||||
cd ~/project
|
||||
@@ -525,8 +579,10 @@ resource "coder_agent" "main" {
|
||||
ARGS="$DEPLOY_ENV"
|
||||
fi
|
||||
|
||||
# Run Claude Code in non-interactive mode
|
||||
claude --print --dangerously-skip-permissions "/project:$TASK_TYPE $ARGS" 2>&1 | tee ~/task-output.log
|
||||
# Run Claude Code in non-interactive mode with tool access
|
||||
claude -p --dangerously-skip-permissions --verbose "/project:$TASK_TYPE $ARGS" 2>&1 | tee ~/task-output.log
|
||||
EXIT_CODE=$?
|
||||
echo "Claude exited with code: $EXIT_CODE" | tee -a ~/task-output.log
|
||||
|
||||
echo "Task completed. Output saved to ~/task-output.log"
|
||||
|
||||
@@ -540,6 +596,21 @@ resource "coder_agent" "main" {
|
||||
EOT
|
||||
}
|
||||
|
||||
resource "coder_app" "task_log" {
|
||||
agent_id = coder_agent.main.id
|
||||
slug = "task-log"
|
||||
display_name = "Task Log"
|
||||
icon = "/icon/document.svg"
|
||||
url = "http://localhost:13338"
|
||||
share = "owner"
|
||||
|
||||
healthcheck {
|
||||
url = "http://localhost:13338"
|
||||
interval = 10
|
||||
threshold = 3
|
||||
}
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# Main Workspace Deployment
|
||||
# =============================================================================
|
||||
@@ -620,7 +691,7 @@ resource "kubernetes_deployment_v1" "workspace" {
|
||||
|
||||
container {
|
||||
name = "coder-agent"
|
||||
image = "codercom/enterprise-base:ubuntu-arm64"
|
||||
image = "codercom/enterprise-base:latest"
|
||||
|
||||
command = ["sh", "-c", coder_agent.main.init_script]
|
||||
|
||||
|
||||
+53
-3
@@ -50,6 +50,38 @@ export class CoderClient {
|
||||
},
|
||||
);
|
||||
|
||||
// Handle 409 conflict — workspace with this name already exists.
|
||||
// This happens when the orchestrator restarts and loses in-memory state.
|
||||
// Only delete if the workspace is old (>30 min) or stopped/failed.
|
||||
// If it's young and running, it may be mid-task — skip to avoid data loss.
|
||||
if (res.status === 409) {
|
||||
const existing = await this.findWorkspaceByName(name);
|
||||
if (!existing) {
|
||||
throw new Error(`Coder 409 but workspace "${name}" not found — possible race condition`);
|
||||
}
|
||||
|
||||
const ageMs = Date.now() - new Date(existing.createdAt).getTime();
|
||||
const ageMin = Math.round(ageMs / 60_000);
|
||||
const stoppedStatuses = ["stopped", "failed", "canceled", "deleted"];
|
||||
const isStopped = stoppedStatuses.includes(existing.latestBuildStatus);
|
||||
const isStale = ageMin > 30;
|
||||
|
||||
if (isStopped || isStale) {
|
||||
console.log(
|
||||
`[coder] workspace "${name}" already exists (age: ${ageMin}m, status: ${existing.latestBuildStatus}) — deleting and retrying`,
|
||||
);
|
||||
await this.deleteWorkspace(existing.id);
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
return this.createWorkspace(task);
|
||||
}
|
||||
|
||||
// Workspace is young and still running — don't kill it
|
||||
console.log(
|
||||
`[coder] workspace "${name}" is active (age: ${ageMin}m, status: ${existing.latestBuildStatus}) — skipping creation`,
|
||||
);
|
||||
return { id: existing.id, name: existing.name };
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`Coder API error ${res.status}: ${text}`);
|
||||
@@ -95,7 +127,12 @@ export class CoderClient {
|
||||
}
|
||||
}
|
||||
|
||||
async findWorkspaceByName(name: string): Promise<{ id: string } | null> {
|
||||
async findWorkspaceByName(name: string): Promise<{
|
||||
id: string;
|
||||
name: string;
|
||||
createdAt: string;
|
||||
latestBuildStatus: string;
|
||||
} | null> {
|
||||
const res = await fetch(
|
||||
`${this.baseUrl}/api/v2/workspaces?q=name:${encodeURIComponent(name)}`,
|
||||
{
|
||||
@@ -107,8 +144,21 @@ export class CoderClient {
|
||||
|
||||
if (!res.ok) return null;
|
||||
|
||||
const data = (await res.json()) as { workspaces: Array<{ id: string; name: string }> };
|
||||
const data = (await res.json()) as {
|
||||
workspaces: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
created_at: string;
|
||||
latest_build: { status: string };
|
||||
}>;
|
||||
};
|
||||
const match = data.workspaces?.find((w) => w.name === name);
|
||||
return match ? { id: match.id } : null;
|
||||
if (!match) return null;
|
||||
return {
|
||||
id: match.id,
|
||||
name: match.name,
|
||||
createdAt: match.created_at,
|
||||
latestBuildStatus: match.latest_build?.status ?? "unknown",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user