Skip to content

Commit 9d682b8

Browse files
Copilottnaum-ms
andcommitted
Add demonstration script for task implementations
Co-authored-by: tnaum-ms <171359267+tnaum-ms@users.noreply.github.com>
1 parent 8cf2c23 commit 9d682b8

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

src/services/tasks/demo.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/**
7+
* Example usage demonstration for DummyTask and PausableDummyTask
8+
*
9+
* This file shows how to use the task implementations for future development.
10+
* It is not part of the production code but serves as documentation.
11+
*/
12+
13+
import { TaskService } from '../taskService';
14+
import { DummyTask, PausableDummyTask } from './index';
15+
16+
/**
17+
* Example: Basic DummyTask usage
18+
*/
19+
export async function demonstrateDummyTask(): Promise<void> {
20+
console.log('=== DummyTask Demonstration ===');
21+
22+
const task = new DummyTask('Demo Dummy Task');
23+
24+
// Register with task service
25+
TaskService.registerTask(task);
26+
27+
console.log('Initial status:', task.getStatus());
28+
29+
// Start the task
30+
await task.start();
31+
console.log('After start:', task.getStatus());
32+
33+
// Monitor progress for a few seconds
34+
const progressInterval = setInterval(() => {
35+
const status = task.getStatus();
36+
console.log(`Progress: ${status.progress}% - ${status.message}`);
37+
38+
if (status.state === 'completed' || status.state === 'stopped') {
39+
clearInterval(progressInterval);
40+
console.log('Final status:', status);
41+
}
42+
}, 2000);
43+
44+
// Stop after 5 seconds
45+
setTimeout(async () => {
46+
await task.stop();
47+
console.log('Task stopped manually');
48+
clearInterval(progressInterval);
49+
await TaskService.deleteTask(task.id);
50+
}, 5000);
51+
}
52+
53+
/**
54+
* Example: PausableDummyTask usage with pause/resume
55+
*/
56+
export async function demonstratePausableTask(): Promise<void> {
57+
console.log('\n=== PausableDummyTask Demonstration ===');
58+
59+
const task = new PausableDummyTask('Demo Pausable Task');
60+
61+
// Register with task service
62+
TaskService.registerTask(task);
63+
64+
console.log('Initial status:', task.getStatus());
65+
66+
// Start the task
67+
await task.start();
68+
console.log('After start:', task.getStatus());
69+
70+
// Let it run for 3 seconds
71+
setTimeout(async () => {
72+
if (task.canPause()) {
73+
await task.pause();
74+
console.log('Task paused:', task.getStatus());
75+
76+
// Resume after 2 seconds of being paused
77+
setTimeout(async () => {
78+
await task.resume();
79+
console.log('Task resumed:', task.getStatus());
80+
}, 2000);
81+
}
82+
}, 3000);
83+
84+
// Monitor progress
85+
const progressInterval = setInterval(() => {
86+
const status = task.getStatus();
87+
console.log(`State: ${status.state}, Progress: ${status.progress}% - ${status.message}`);
88+
89+
if (status.state === 'completed' || status.state === 'stopped') {
90+
clearInterval(progressInterval);
91+
console.log('Final status:', status);
92+
void TaskService.deleteTask(task.id);
93+
}
94+
}, 1000);
95+
}
96+
97+
/**
98+
* Example: Using TaskService to manage multiple tasks
99+
*/
100+
export async function demonstrateTaskService(): Promise<void> {
101+
console.log('\n=== TaskService Demonstration ===');
102+
103+
const task1 = new DummyTask('Task 1');
104+
const task2 = new PausableDummyTask('Pausable Task 2');
105+
106+
// Register multiple tasks
107+
TaskService.registerTask(task1);
108+
TaskService.registerTask(task2);
109+
110+
console.log('Registered tasks:', TaskService.listTasks().map(t => ({ id: t.id, name: t.name, type: t.type })));
111+
112+
// Start both tasks
113+
await task1.start();
114+
await task2.start();
115+
116+
// Demonstrate service operations
117+
console.log('Task 1 pausable?', TaskService.isTaskPausable(task1.id));
118+
console.log('Task 2 pausable?', TaskService.isTaskPausable(task2.id));
119+
120+
// Pause the pausable task via service
121+
if (TaskService.isTaskPausable(task2.id)) {
122+
setTimeout(async () => {
123+
await TaskService.pauseTask(task2.id);
124+
console.log('Paused task 2 via service');
125+
126+
setTimeout(async () => {
127+
await TaskService.resumeTask(task2.id);
128+
console.log('Resumed task 2 via service');
129+
}, 2000);
130+
}, 2000);
131+
}
132+
133+
// Clean up after 8 seconds
134+
setTimeout(async () => {
135+
console.log('Cleaning up tasks...');
136+
await TaskService.deleteTask(task1.id);
137+
await TaskService.deleteTask(task2.id);
138+
console.log('All tasks cleaned up');
139+
}, 8000);
140+
}
141+
142+
// Export for potential use in integration demos
143+
export { DummyTask, PausableDummyTask, TaskService };

0 commit comments

Comments
 (0)