dt-common-device
Version:
A secure and robust device management library for IoT applications
141 lines (140 loc) • 5.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CopilotQueueExample = void 0;
const typedi_1 = require("typedi");
const index_1 = require("../index");
/**
* Example usage of CopilotQueueService
* This demonstrates how to use the queue system to handle API requests with FIFO ordering per property
*/
class CopilotQueueExample {
constructor() {
// Get the service instance from the DI container
this.copilotQueueService = typedi_1.Container.get(index_1.CopilotQueueService);
}
/**
* Example: Add a simple GET request to the queue
*/
async addGetRequest(propertyId, url) {
const request = {
url,
method: 'GET',
propertyId,
headers: {
'Authorization': 'Bearer your-token-here',
'Accept': 'application/json',
},
};
const result = await this.copilotQueueService.addQueueRequest(request);
console.log(`GET request queued with job ID: ${result.jobId}`);
return result.jobId;
}
/**
* Example: Add a POST request with body to the queue
*/
async addPostRequest(propertyId, url, data) {
const request = {
url,
method: 'POST',
propertyId,
headers: {
'Authorization': 'Bearer your-token-here',
'Content-Type': 'application/json',
},
body: data,
};
const options = {
priority: 1, // Higher priority
attempts: 5, // Retry up to 5 times
backoff: {
type: 'exponential',
delay: 3000,
},
};
const result = await this.copilotQueueService.addQueueRequest(request, options);
console.log(`POST request queued with job ID: ${result.jobId}`);
return result.jobId;
}
/**
* Example: Add multiple requests for different properties
* This demonstrates FIFO ordering per property
*/
async addMultipleRequests() {
const requests = [
{ propertyId: 'property-1', url: 'https://api.example.com/endpoint1', method: 'GET' },
{ propertyId: 'property-2', url: 'https://api.example.com/endpoint2', method: 'POST', body: { data: 'test' } },
{ propertyId: 'property-1', url: 'https://api.example.com/endpoint3', method: 'PUT', body: { update: 'data' } },
{ propertyId: 'property-2', url: 'https://api.example.com/endpoint4', method: 'GET' },
{ propertyId: 'property-1', url: 'https://api.example.com/endpoint5', method: 'DELETE' },
];
const jobIds = [];
for (const req of requests) {
const request = {
url: req.url,
method: req.method,
propertyId: req.propertyId,
headers: {
'Authorization': 'Bearer your-token-here',
},
body: req.body,
};
const result = await this.copilotQueueService.addQueueRequest(request);
jobIds.push(result.jobId);
console.log(`Request queued for ${req.propertyId}: ${req.method} ${req.url} (Job ID: ${result.jobId})`);
}
console.log('All requests queued. Jobs will be processed in FIFO order per property.');
}
/**
* Example: Monitor job status
*/
async monitorJob(jobId) {
const status = await this.copilotQueueService.getJobStatus(jobId);
console.log('Job Status:', status);
}
/**
* Example: Get all jobs for a specific property
*/
async getPropertyJobs(propertyId) {
const jobs = await this.copilotQueueService.getJobsByProperty(propertyId);
console.log(`Jobs for property ${propertyId}:`, jobs);
}
/**
* Example: Cancel a job
*/
async cancelJob(jobId) {
const cancelled = await this.copilotQueueService.cancelJob(jobId);
console.log(`Job ${jobId} cancelled: ${cancelled}`);
}
/**
* Example: Graceful shutdown
*/
async shutdown() {
await this.copilotQueueService.shutdown();
console.log('CopilotQueue service shutdown complete');
}
}
exports.CopilotQueueExample = CopilotQueueExample;
// Usage example
async function runExample() {
const example = new CopilotQueueExample();
try {
// Add some requests
await example.addMultipleRequests();
// Wait a bit for processing
await new Promise(resolve => setTimeout(resolve, 5000));
// Check job status
const jobId = await example.addGetRequest('property-1', 'https://api.example.com/test');
await example.monitorJob(jobId);
// Get jobs for a property
await example.getPropertyJobs('property-1');
}
catch (error) {
console.error('Example error:', error);
}
finally {
// Cleanup
await example.shutdown();
}
}
// Uncomment to run the example
// runExample();