Skip to content

Commit

Permalink
Bug fix: connector control
Browse files Browse the repository at this point in the history
Signed-off-by: EkaterinaIvanishcheva <140404900+EkaterinaIvanishcheva@users.noreply.github.com>
  • Loading branch information
EkaterinaIvanishcheva committed Jul 14, 2024
1 parent aa2dd29 commit c395a4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/frontend/app/api/check_status/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { NextResponse } from 'next/server';
import { execSync } from 'child_process';
import { auth } from "@/auth"

const executeCommand = (command: string) => {
try {
const output = execSync(command, { encoding: 'utf-8' });
console.log(output);
return output.trim(); // Return the trimmed output
} catch (error) {
console.error(error);
throw new Error((error as Error).message);
}
};

function getConnectorStatusUrl(connectorName: string | null) {
if (process.env.RUNNING_ENV == "local") {
return "http://" + connectorName + ":19191/connector-api/status";
Expand All @@ -12,6 +24,11 @@ function getConnectorStatusUrl(connectorName: string | null) {
async function checkConnectorStatus(connectorName: string | null): Promise<boolean> {
const url = getConnectorStatusUrl(connectorName);
try {
let containerID;
containerID = executeCommand("docker container ls -f 'status=paused' | grep 'src-" + connectorName + "-1' | awk '{print $1}'");
if (containerID) { // Checks if connector is paused, otherwise fetch will hang
return false;
}
console.log("Trying to fetch connector status from URL " + url);
var result = await fetch(url, {cache: "no-store"});
var data = await result.json();
Expand Down
21 changes: 12 additions & 9 deletions src/frontend/app/dashboard/change_status.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState, useEffect } from 'react';
import { PauseCircleIcon, PlayCircleIcon } from '@heroicons/react/24/outline';
import { PauseCircleIcon, PlayCircleIcon, ExclamationCircleIcon } from '@heroicons/react/24/outline';
import { isConnectorRunning } from '@/actions/api';

interface ConnectorStatusProps {
connectorName: string | undefined;
}

export default function ChangeStatusButton({ connectorName }: ConnectorStatusProps) {
const [buttonText, setButtonText] = useState<string>('Checking status...');
const [iconName, setIconName] = useState<string>('PauseCircleIcon');
const [iconName, setIconName] = useState<string>('ExclamationCircleIcon');
const [buttonColor, setButtonColor] = useState<string>('bg-green-500');

const iconMapping: { [key: string]: React.ElementType } = {
Expand All @@ -19,9 +20,8 @@ export default function ChangeStatusButton({ connectorName }: ConnectorStatusPro
if (!connectorName) return;

try {
const response = await fetch(`/api/check_status?connector=${connectorName}`);
const data = await response.json();
if (data.status === 'running') {
const connectorRunningResponse = await isConnectorRunning(connectorName || "");
if (connectorRunningResponse) {
await fetch('/api/pauseConnector');
setButtonText("Start the connector");
setIconName("PlayCircleIcon");
Expand All @@ -34,6 +34,8 @@ export default function ChangeStatusButton({ connectorName }: ConnectorStatusPro
}
} catch (error) {
setButtonText('Error checking status');
setIconName("ExclamationCircleIcon");
setButtonColor('bg-red-500');
}
};

Expand All @@ -42,9 +44,8 @@ export default function ChangeStatusButton({ connectorName }: ConnectorStatusPro
if (!connectorName) return;

try {
const response = await fetch(`/api/check_status?connector=${connectorName}`);
const data = await response.json();
if (data.status === 'running') {
const connectorRunningResponse = await isConnectorRunning(connectorName || "");
if (connectorRunningResponse) {
setButtonText("Pause the connector");
setIconName("PauseCircleIcon");
setButtonColor('bg-red-500');
Expand All @@ -55,13 +56,15 @@ export default function ChangeStatusButton({ connectorName }: ConnectorStatusPro
}
} catch (error) {
setButtonText('Error checking status');
setIconName("ExclamationCircleIcon");
setButtonColor('bg-red-500');
}
};

fetchInitialStatus();
}, [connectorName]);

const IconComponent = iconMapping[iconName] || PauseCircleIcon;
const IconComponent = iconMapping[iconName] || ExclamationCircleIcon;

return (
<div className="flex justify-start pb-5">
Expand Down

0 comments on commit c395a4a

Please sign in to comment.