fix(bridge): prevent WriterConfigService from crashing on EACCES

The module-level singleton called mkdirSync('config/writers') which
fatally crashed the bridge when running as www-data without write
permission to the working directory. The bridge has been crash-looping
with restart counter >3600, taking andebol.net offline.

Constructor now catches EACCES gracefully (warn + degrade) instead of
killing the process. Deploy scripts also ensure the directory exists
with correct ownership.

Paperclip-Agent: peter-spark
Paperclip-Issue: BIT-743
Paperclip-Run: 817ec0b4-3778-44ae-8c07-efd97a2bdd86
This commit is contained in:
Artur Moreira committed 2026-04-02 23:28:50 +00:00
1 parent 89040e8a15
commit cccf26f657
3 files changed
+19 -3

No files matched your search

+15 -3
View File
@@ -31,12 +31,20 @@ const WRITERS_CONFIG_DIR = process.env['WRITERS_CONFIG_DIR'] || 'config/writers'
export class WriterConfigService {
private configDir: string;
private readonly available: boolean;
constructor(configDir?: string) {
this.configDir = configDir || WRITERS_CONFIG_DIR;
// Ensure directory exists
if (!existsSync(this.configDir)) {
mkdirSync(this.configDir, { recursive: true });
try {
if (!existsSync(this.configDir)) {
mkdirSync(this.configDir, { recursive: true });
}
this.available = true;
} catch {
console.warn(
`[WriterConfigService] Cannot create ${this.configDir} — writer config disabled (read-only deployment?)`,
);
this.available = false;
}
}
@@ -45,6 +53,7 @@ export class WriterConfigService {
}
loadConfig(agentId: string): WriterConfig | null {
if (!this.available) return null;
const path = this.getConfigPath(agentId);
if (!existsSync(path)) {
return null;
@@ -58,6 +67,9 @@ export class WriterConfigService {
}
saveConfig(config: WriterConfig): void {
if (!this.available) {
throw new Error('WriterConfigService is not available (config directory not writable)');
}
const path = this.getConfigPath(config.agent_id);
config.updated_at = new Date().toISOString();
try {
+2
View File
@@ -106,6 +106,8 @@ done
# --- Ownership ---
chown -R www-data:www-data "$APP_DIR/frontend/dist" 2>/dev/null || true
chown -R www-data:www-data "$APP_DIR/bridge/dist" 2>/dev/null || true
mkdir -p "$APP_DIR/bridge/config/writers"
chown www-data:www-data "$APP_DIR/bridge/config/writers"
log ""
log "=== Deployment complete ==="
+2
View File
@@ -50,6 +50,8 @@ if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^bridge/"; then
cd "$APP_DIR/bridge"
npm ci --silent
npm run build
mkdir -p "$APP_DIR/bridge/config/writers"
chown www-data:www-data "$APP_DIR/bridge/config/writers"
systemctl restart news-bridge
log "Bridge restarted."
fi