fix: clean up SmartProxy lifecycle
This commit is contained in:
@@ -10,6 +10,7 @@ import { logger } from '../logging.ts';
|
||||
import { getErrorMessage } from '../utils/error.ts';
|
||||
|
||||
const SMARTPROXY_SERVICE_NAME = 'onebox-smartproxy';
|
||||
const LEGACY_CADDY_SERVICE_NAME = 'onebox-caddy';
|
||||
const SMARTPROXY_IMAGE = 'code.foss.global/host.today/ht-docker-smartproxy:latest';
|
||||
const SMARTPROXY_ADMIN_CONTAINER_PORT = 3000;
|
||||
const SMARTPROXY_HTTP_CONTAINER_PORT = 80;
|
||||
@@ -101,6 +102,13 @@ export class SmartProxyManager {
|
||||
|
||||
logger.info('Starting SmartProxy Docker service...');
|
||||
|
||||
const legacyService = await this.getExistingService(LEGACY_CADDY_SERVICE_NAME);
|
||||
if (legacyService) {
|
||||
logger.info('Legacy Caddy service exists, removing it before SmartProxy startup...');
|
||||
await this.removeService(LEGACY_CADDY_SERVICE_NAME);
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
}
|
||||
|
||||
const existingService = await this.getExistingService();
|
||||
if (existingService) {
|
||||
logger.info('SmartProxy service exists, removing old service...');
|
||||
@@ -180,9 +188,9 @@ export class SmartProxyManager {
|
||||
}
|
||||
}
|
||||
|
||||
private async getExistingService(): Promise<any | null> {
|
||||
private async getExistingService(serviceNameArg = SMARTPROXY_SERVICE_NAME): Promise<any | null> {
|
||||
try {
|
||||
const response = await this.dockerClient!.request('GET', `/services/${SMARTPROXY_SERVICE_NAME}`, {});
|
||||
const response = await this.dockerClient!.request('GET', `/services/${serviceNameArg}`, {});
|
||||
if (response.statusCode === 200) {
|
||||
return response.body;
|
||||
}
|
||||
@@ -192,9 +200,9 @@ export class SmartProxyManager {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeService(): Promise<void> {
|
||||
private async removeService(serviceNameArg = SMARTPROXY_SERVICE_NAME): Promise<void> {
|
||||
try {
|
||||
await this.dockerClient!.request('DELETE', `/services/${SMARTPROXY_SERVICE_NAME}`, {});
|
||||
await this.dockerClient!.request('DELETE', `/services/${serviceNameArg}`, {});
|
||||
} catch {
|
||||
// Service may not exist.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user