Skip to content

Commit d5fee3e

Browse files
final commit for start stop server
1 parent 5fbcd7e commit d5fee3e

File tree

7 files changed

+50
-9
lines changed

7 files changed

+50
-9
lines changed

src/commander/ping.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ command
1919
console.log(chalk.yellow(`Pinging server at ${serverAddress} from terminal...`));
2020

2121
// Send GET request to the /ping endpoint
22-
const response = await axios.get(`${serverAddress}/ping`);
22+
const response = await axios.get(`${serverAddress}/ping`, { timeout: 15000 });
2323

2424
// Log the response from the server
2525
if (response.status === 200) {
@@ -30,7 +30,11 @@ command
3030
}
3131
} catch (error: any) {
3232
// Handle any errors during the HTTP request
33-
console.error(chalk.red('SmartUI server is not running'));
33+
if (error.code === 'ECONNABORTED') {
34+
console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds'));
35+
} else {
36+
console.error(chalk.red('SmartUI server is not running'));
37+
}
3438
}
3539
});
3640

src/commander/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ctxInit from '../lib/ctx.js';
77
import getGitInfo from '../tasks/getGitInfo.js';
88
import createBuild from '../tasks/createBuild.js';
99
import snapshotQueue from '../lib/snapshotQueue.js';
10-
import { startPolling } from '../lib/utils.js';
10+
import { startPolling, startPingPolling } from '../lib/utils.js';
1111

1212
const command = new Command();
1313

@@ -26,6 +26,7 @@ command
2626
let ctx: Context = ctxInit(command.optsWithGlobals());
2727
ctx.snapshotQueue = new snapshotQueue(ctx);
2828
ctx.totalSnapshots = 0
29+
ctx.isStartExec = true
2930

3031
let tasks = new Listr<Context>(
3132
[
@@ -49,6 +50,7 @@ command
4950

5051
try {
5152
await tasks.run(ctx);
53+
startPingPolling(ctx);
5254
if (ctx.options.fetchResults) {
5355
startPolling(ctx);
5456
}

src/lib/ctx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default (options: Record<string, string>): Context => {
126126
fetchResultsFileName: fetchResultsFileObj,
127127
},
128128
cliVersion: version,
129-
totalSnapshots: -1
129+
totalSnapshots: -1,
130+
isStartExec: false
130131
}
131132
}

src/lib/httpClient.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ export default class httpClient {
8484
}
8585
}
8686

87-
createBuild(git: Git, config: any, log: Logger, buildName: string) {
87+
createBuild(git: Git, config: any, log: Logger, buildName: string, isStartExec: boolean) {
8888
return this.request({
8989
url: '/build',
9090
method: 'POST',
9191
data: {
9292
git,
9393
config,
94-
buildName
94+
buildName,
95+
isStartExec
9596
}
9697
}, log)
9798
}
@@ -102,7 +103,18 @@ export default class httpClient {
102103
method: 'GET',
103104
params: { buildId, baseline }
104105
}, log);
105-
}
106+
}
107+
108+
ping(buildId: string, log: Logger) {
109+
return this.request({
110+
url: '/build/ping',
111+
method: 'POST',
112+
data: {
113+
buildId: buildId
114+
}
115+
}, log);
116+
}
117+
106118

107119
finalizeBuild(buildId: string, totalSnapshots: number, log: Logger) {
108120
let params: Record<string, string | number> = {buildId};

src/lib/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,25 @@ export async function startPolling(ctx: Context): Promise<void> {
307307
isPollingActive = false;
308308
}
309309
}, 5000);
310-
}
310+
}
311+
312+
export async function startPingPolling(ctx: Context): Promise<void> {
313+
try {
314+
ctx.log.debug('Sending initial ping to server...');
315+
await ctx.client.ping(ctx.build.id, ctx.log);
316+
ctx.log.debug('Initial ping sent successfully.');
317+
} catch (error: any) {
318+
ctx.log.error(`Error during initial ping: ${error.message}`);
319+
}
320+
321+
setInterval(async () => {
322+
try {
323+
ctx.log.debug('Sending ping to server...');
324+
await ctx.client.ping(ctx.build.id, ctx.log);
325+
ctx.log.debug('Ping sent successfully.');
326+
} catch (error: any) {
327+
ctx.log.error(`Error during ping polling: ${error.message}`);
328+
}
329+
}, 10 * 60 * 1000);
330+
}
331+

src/tasks/createBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1010
updateLogContext({task: 'createBuild'});
1111

1212
try {
13-
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name);
13+
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec);
1414
ctx.build = {
1515
id: resp.data.buildId,
1616
name: resp.data.buildName,

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface Context {
5252
totalSnapshots: number;
5353
figmaDesignConfig?: FigmaDesignConfig;
5454
testType?: string;
55+
isStartExec ?: boolean;
5556
}
5657

5758
export interface Env {

0 commit comments

Comments
 (0)