@@ -9,6 +9,8 @@ import * as fs from 'fs';
99
1010@Injectable ( )
1111export class PowerAvailabilityService {
12+ private isUpdateBlocked = true ;
13+
1214 constructor (
1315 @Inject ( WINSTON_LOGGER ) private readonly logger : Logger ,
1416 @InjectRepository ( PowerAvailability )
@@ -19,7 +21,9 @@ export class PowerAvailabilityService {
1921 this . logger . info (
2022 `[${ PowerAvailabilityService . name } ].${ this . processApplicationStart . name } => Start` ,
2123 ) ;
22- let powerAvailability ;
24+
25+ let powerAvailability : PowerAvailability ;
26+
2327 if ( fs . existsSync ( 'server.off' ) ) {
2428 this . logger . info (
2529 `[${ PowerAvailabilityService . name } ].${ this . processApplicationStart . name } => Server stopped correctly.` ,
@@ -37,28 +41,41 @@ export class PowerAvailabilityService {
3741 `[${ PowerAvailabilityService . name } ].${ this . processApplicationStart . name } => Server stopped unexpectedly.` ,
3842 ) ;
3943 }
44+
4045 if ( ! powerAvailability ) {
4146 powerAvailability = new PowerAvailability ( ) ;
4247 }
48+
4349 powerAvailability . updated = new Date ( ) ;
50+
4451 await this . powerAvailabilityRepository . save ( powerAvailability ) ;
52+
4553 this . logger . info (
4654 `[${ PowerAvailabilityService . name } ].${ this . processApplicationStart . name } => Finish` ,
4755 ) ;
56+
57+ this . isUpdateBlocked = false ;
4858 }
4959
5060 async updatePowerAvailability ( ) {
61+ if ( this . isUpdateBlocked ) {
62+ return ;
63+ }
5164 const maxId = await this . powerAvailabilityRepository
5265 . createQueryBuilder ( 'pa' )
5366 . select ( 'MAX(pa.id)' , 'max' )
5467 . getRawOne ( ) ;
68+
5569 let powerAvailability = await this . powerAvailabilityRepository . findOne ( {
5670 where : { id : maxId . max } ,
5771 } ) ;
72+
5873 if ( ! powerAvailability ) {
5974 powerAvailability = new PowerAvailability ( ) ;
6075 }
76+
6177 powerAvailability . updated = new Date ( ) ;
78+
6279 await this . powerAvailabilityRepository . save ( powerAvailability ) ;
6380 }
6481
0 commit comments