Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion packages/cli/src/admin/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
Log,
Platform,
getOrderIdByName,
getOrderIdByRepositoryUrl,
remakeOrder,
waitForOrder
} from '@osaas/client-core';
import {
getServiceRepoUrl,
getSyncForkStatus,
triggerSyncFork
triggerSyncFork,
waitForSyncFork
} from './syncfork';
import {
getInstancesToRemove,
Expand All @@ -25,6 +27,8 @@ import {
} from './subscription';
import { getTenantPlanMap, getTenantTokenCounts } from './money';

const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));

export default function cmdAdmin() {
const admin = new Command('admin');
admin
Expand Down Expand Up @@ -267,6 +271,7 @@ export default function cmdAdmin() {
admin
.command('sync-fork')
.description('Trigger a fork sync for an OSC service')
.option('-w, --wait', 'Wait for sync fork and remake to complete')
.argument('<serviceId>', 'The OSC service ID (e.g. eyevinn-web-runner)')
.action(async (serviceId, options, command) => {
try {
Expand Down Expand Up @@ -304,6 +309,53 @@ export default function cmdAdmin() {
return;
}
}

if (options.wait) {
Log().info('Waiting for sync fork to complete...');
const finalSyncStatus = await waitForSyncFork(repoUrl, platform);
if (finalSyncStatus?.status === 'failed') {
console.log(`Sync fork status: ${finalSyncStatus.status}`);
if (finalSyncStatus.error) {
console.log(
`Sync fork failed: [${finalSyncStatus.error.type}] ${finalSyncStatus.error.message}`
);
if (finalSyncStatus.error.conflictFiles?.length) {
console.log(
`Conflict files: ${finalSyncStatus.error.conflictFiles.join(
', '
)}`
);
}
}
return;
}

const maxAttempts = 24;
const pollIntervalMs = 5000;
let orderId: string | undefined;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
orderId = await getOrderIdByRepositoryUrl(platform, repoUrl);
if (orderId) {
break;
}
await delay(pollIntervalMs);
}

if (!orderId) {
Log().error(
`No remake order found for ${repoUrl} after waiting ${
(maxAttempts * pollIntervalMs) / 1000
}s — it may not have started yet; check with 'osc admin remake-order' once it appears`
);
return;
}

Log().info('Waiting for remake to complete...');
await waitForOrder(platform, orderId);
Log().info(
`Sync fork and remake for ${serviceId} completed successfully`
);
}
} catch (err) {
console.log((err as Error).message);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/src/admin/syncfork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,25 @@ export async function getSyncForkStatus(
throw err;
}
}

const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));

export async function waitForSyncFork(
repositoryUrl: string,
platform: Platform
): Promise<SyncForkStatus | undefined> {
let running = true;
while (running) {
const status = await getSyncForkStatus(repositoryUrl, platform);
if (!status) {
return undefined;
}
if (status.status === 'pending' || status.status === 'running') {
await delay(5000);
} else {
running = false;
return status;
}
}
return undefined;
}
37 changes: 37 additions & 0 deletions packages/core/src/platform/maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ export async function getOrderIdByName(platform: Platform, orderName: string) {
}
}

const ACTIVE_ORDER_PHASES = new Set(['NEW', 'PENDING', 'RUNNING']);

export async function getOrderIdByRepositoryUrl(
platform: Platform,
repositoryUrl: string
) {
try {
const makerUrl = new URL(
`https://maker.svc.${platform.getEnvironment()}.osaas.io/maker`
);
makerUrl.searchParams.append('repositoryUrl', repositoryUrl);
const res = await createFetch<OutputOrder[]>(makerUrl, {
headers: {
Authorization: `Bearer ${platform.getApiKey()}`,
'Content-Type': 'application/json'
}
});
const activeOrder = res.find((o) =>
ACTIVE_ORDER_PHASES.has(o.order.status?.phase || '')
);
if (activeOrder) {
return activeOrder.order.orderId;
}
if (res[0]) {
return res[0].order.orderId;
}
return undefined;
} catch (err) {
Log().debug(err);
if (err instanceof FetchError && err.httpCode === 401) {
throw new UnauthorizedError();
} else if (err instanceof FetchError && err.httpCode === 404) {
return undefined;
}
}
}

export async function waitForOrder(platform: Platform, orderId: string) {
try {
let running = true;
Expand Down
Loading