mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00

- Added SchedulerModule and SchedulerService to handle hourly data updates for AQI, occupancy, and energy consumption. - Refactored existing services to remove unused device repository dependencies and streamline procedure execution. - Updated SQL procedures to use correct parameter indexing. - Enhanced error handling and logging for scheduled tasks. - Integrated new repositories for presence sensor and AQI pollutant stats across multiple modules. - Added NestJS schedule package for task scheduling capabilities.
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
|
|
|
@Injectable()
|
|
export class SchedulerService {
|
|
constructor(
|
|
private readonly powerClampService: PowerClampService,
|
|
private readonly occupancyService: OccupancyService,
|
|
private readonly aqiDataService: AqiDataService,
|
|
) {
|
|
console.log('SchedulerService initialized!');
|
|
}
|
|
|
|
@Cron(CronExpression.EVERY_HOUR)
|
|
async runHourlyProcedures() {
|
|
console.log('\n======== Starting Procedures ========');
|
|
console.log(new Date().toISOString(), 'Scheduler running...');
|
|
|
|
try {
|
|
const results = await Promise.allSettled([
|
|
this.executeTask(
|
|
() => this.powerClampService.updateEnergyConsumedHistoricalData(),
|
|
'Energy Consumption',
|
|
),
|
|
this.executeTask(
|
|
() => this.occupancyService.updateOccupancyDataProcedures(),
|
|
'Occupancy Data',
|
|
),
|
|
this.executeTask(
|
|
() => this.aqiDataService.updateAQISensorHistoricalData(),
|
|
'AQI Data',
|
|
),
|
|
]);
|
|
|
|
this.logResults(results);
|
|
} catch (error) {
|
|
console.error('MAIN SCHEDULER ERROR:', error);
|
|
if (error.stack) {
|
|
console.error('Error stack:', error.stack);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async executeTask(
|
|
task: () => Promise<void>,
|
|
name: string,
|
|
): Promise<{ name: string; status: string }> {
|
|
try {
|
|
console.log(`[${new Date().toISOString()}] Starting ${name} task...`);
|
|
await task();
|
|
console.log(
|
|
`[${new Date().toISOString()}] ${name} task completed successfully`,
|
|
);
|
|
return { name, status: 'success' };
|
|
} catch (error) {
|
|
console.error(
|
|
`[${new Date().toISOString()}] ${name} task failed:`,
|
|
error.message,
|
|
);
|
|
if (error.stack) {
|
|
console.error('Task error stack:', error.stack);
|
|
}
|
|
return { name, status: 'failed' };
|
|
}
|
|
}
|
|
|
|
private logResults(results: PromiseSettledResult<any>[]) {
|
|
const successCount = results.filter((r) => r.status === 'fulfilled').length;
|
|
const failedCount = results.length - successCount;
|
|
|
|
console.log('\n======== Task Results ========');
|
|
console.log(`Successful tasks: ${successCount}`);
|
|
console.log(`Failed tasks: ${failedCount}`);
|
|
|
|
if (failedCount > 0) {
|
|
console.log('\n======== Failed Tasks Details ========');
|
|
results.forEach((result, index) => {
|
|
if (result.status === 'rejected') {
|
|
console.error(`Task ${index + 1} failed:`, result.reason);
|
|
if (result.reason.stack) {
|
|
console.error('Error stack:', result.reason.stack);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
console.log('\n======== Scheduler Completed ========\n');
|
|
}
|
|
}
|