Просмотр исходного кода

Use UTC milliseconds for cache expiredAt timestamp

Taichi Masuyama 4 лет назад
Родитель
Сommit
9ba366661a

+ 2 - 2
packages/slackbot-proxy/src/controllers/growi-to-slack.ts

@@ -3,7 +3,6 @@ import {
 } from '@tsed/common';
 } from '@tsed/common';
 import axios from 'axios';
 import axios from 'axios';
 import createError from 'http-errors';
 import createError from 'http-errors';
-import { addHours } from 'date-fns';
 
 
 import { WebAPICallResult } from '@slack/web-api';
 import { WebAPICallResult } from '@slack/web-api';
 
 
@@ -186,7 +185,8 @@ export class GrowiToSlackCtrl {
     logger.debug('relation test is success', order);
     logger.debug('relation test is success', order);
 
 
     // temporary cache for 48 hours
     // temporary cache for 48 hours
-    const expiredAtCommands = addHours(new Date(), 48);
+    const nowDate = new Date();
+    const expiredAtCommands = nowDate.getTime() + 48 * 60 * 60 * 1000;
 
 
     // Transaction is not considered because it is used infrequently,
     // Transaction is not considered because it is used infrequently,
     const response = await this.relationRepository.createQueryBuilder('relation')
     const response = await this.relationRepository.createQueryBuilder('relation')

+ 3 - 4
packages/slackbot-proxy/src/entities/relation-mock.ts

@@ -1,7 +1,6 @@
 import {
 import {
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
 } from 'typeorm';
 } from 'typeorm';
-import { differenceInMilliseconds } from 'date-fns';
 import { Installation } from './installation';
 import { Installation } from './installation';
 
 
 
 
@@ -50,11 +49,11 @@ export class RelationMock {
   @Column({ type: 'json' })
   @Column({ type: 'json' })
   permittedChannelsForEachCommand : PermittedChannelsForEachCommand
   permittedChannelsForEachCommand : PermittedChannelsForEachCommand
 
 
-  @CreateDateColumn()
-  expiredAtCommands: Date;
+  @Column({ type: 'bigint' })
+  expiredAtCommands: number;
 
 
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
-    return differenceInMilliseconds(this.expiredAtCommands, baseDate);
+    return this.expiredAtCommands - baseDate.getTime();
   }
   }
 
 
 }
 }

+ 3 - 4
packages/slackbot-proxy/src/entities/relation.ts

@@ -1,7 +1,6 @@
 import {
 import {
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
 } from 'typeorm';
 } from 'typeorm';
-import { differenceInMilliseconds } from 'date-fns';
 import { Installation } from './installation';
 import { Installation } from './installation';
 
 
 @Entity()
 @Entity()
@@ -37,11 +36,11 @@ export class Relation {
   @Column('simple-array')
   @Column('simple-array')
   supportedCommandsForSingleUse: string[];
   supportedCommandsForSingleUse: string[];
 
 
-  @CreateDateColumn()
-  expiredAtCommands: Date;
+  @Column({ type: 'bigint' })
+  expiredAtCommands: number;
 
 
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
-    return differenceInMilliseconds(this.expiredAtCommands, baseDate);
+    return this.expiredAtCommands - baseDate.getTime();
   }
   }
 
 
 }
 }

+ 3 - 3
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -1,6 +1,5 @@
 import { Inject, Service } from '@tsed/di';
 import { Inject, Service } from '@tsed/di';
 import axios from 'axios';
 import axios from 'axios';
-import { addHours } from 'date-fns';
 
 
 import { Relation } from '~/entities/relation';
 import { Relation } from '~/entities/relation';
 import { RelationRepository } from '~/repositories/relation';
 import { RelationRepository } from '~/repositories/relation';
@@ -30,7 +29,8 @@ export class RelationsService {
     const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
     const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
     relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
     relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
     relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
     relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
-    relation.expiredAtCommands = addHours(new Date(), 48);
+    const nowDate = new Date();
+    relation.expiredAtCommands = nowDate.getTime() + 48 * 60 * 60 * 1000;
 
 
     return this.relationRepository.save(relation);
     return this.relationRepository.save(relation);
   }
   }
@@ -49,7 +49,7 @@ export class RelationsService {
     }
     }
 
 
     // 24 hours
     // 24 hours
-    if (distanceMillisecondsToExpiredAt < 1000 * 60 * 60 * 24) {
+    if (distanceMillisecondsToExpiredAt < 24 * 60 * 60 * 1000) {
       try {
       try {
         this.syncSupportedGrowiCommands(relation);
         this.syncSupportedGrowiCommands(relation);
       }
       }