From 6d2edaaaf837f19ba24e1e242dc579f6403efc0c Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 18 Jun 2022 12:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E6=9C=9F=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/cron.ts | 5 ++--- back/services/dependence.ts | 34 ++++++++++++++-------------------- back/services/subscription.ts | 6 +++--- back/services/user.ts | 6 +++--- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/back/services/cron.ts b/back/services/cron.ts index 91ba740b..077a22fc 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -10,6 +10,7 @@ import { promises, existsSync } from 'fs'; import { promisify } from 'util'; import { Op } from 'sequelize'; import path from 'path'; +import dayjs from 'dayjs'; @Service() export default class CronService { @@ -201,9 +202,7 @@ export default class CronService { const str = err ? `\n${err}` : ''; fs.appendFileSync( `${absolutePath}`, - `${str}\n## 执行结束... ${new Date() - .toLocaleString('zh', { hour12: false }) - .replace(' 24:', ' 00:')} `, + `${str}\n## 执行结束... ${dayjs().format('YYYY-MM-DD HH:mm:ss')} `, ); } } diff --git a/back/services/dependence.ts b/back/services/dependence.ts index 7013cf2b..fba1b623 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -14,6 +14,7 @@ import { spawn } from 'child_process'; import SockService from './sock'; import { Op } from 'sequelize'; import { concurrentRun } from '../config/util'; +import dayjs from 'dayjs'; @Service() export default class DependenceService { @@ -161,20 +162,17 @@ export default class DependenceService { )[dependencies[0].type as any]; const actionText = isInstall ? '安装' : '删除'; const depIds = dependencies.map((x) => x.id) as number[]; - const startTime = Date.now(); + const startTime = dayjs(); + + const message = `开始${actionText}依赖 ${depNames},开始时间 ${startTime.format( + 'YYYY-MM-DD HH:mm:ss', + )}\n\n`; this.sockService.sendMessage({ type: socketMessageType, - message: `开始${actionText}依赖 ${depNames},开始时间 ${new Date( - startTime, - ).toLocaleString()}\n\n`, + message, references: depIds, }); - await this.updateLog( - depIds, - `开始${actionText}依赖 ${depNames},开始时间 ${new Date( - startTime, - ).toLocaleString()}\n\n`, - ); + await this.updateLog(depIds, message); const cp = spawn(`${depRunCommand} ${depNames}`, { shell: '/bin/bash' }); @@ -207,23 +205,19 @@ export default class DependenceService { }); cp.on('close', async (code) => { - const endTime = Date.now(); + const endTime = dayjs(); const isSucceed = code === 0; const resultText = isSucceed ? '成功' : '失败'; + const message = `\n依赖${actionText}${resultText},结束时间 ${endTime.format( + 'YYYY-MM-DD HH:mm:ss', + )},耗时 ${endTime.diff(startTime, 'second')} 秒`; this.sockService.sendMessage({ type: socketMessageType, - message: `\n依赖${actionText}${resultText},结束时间 ${new Date( - endTime, - ).toLocaleString()},耗时 ${(endTime - startTime) / 1000} 秒`, + message, references: depIds, }); - await this.updateLog( - depIds, - `\n依赖${actionText}${resultText},结束时间 ${new Date( - endTime, - ).toLocaleString()},耗时 ${(endTime - startTime) / 1000} 秒`, - ); + await this.updateLog(depIds, message); let status = null; if (isSucceed) { diff --git a/back/services/subscription.ts b/back/services/subscription.ts index 8916d0de..264eaf95 100644 --- a/back/services/subscription.ts +++ b/back/services/subscription.ts @@ -28,6 +28,7 @@ import ScheduleService, { TaskCallbacks } from './schedule'; import { SimpleIntervalSchedule } from 'toad-scheduler'; import SockService from './sock'; import SshKeyService from './sshKey'; +import dayjs from 'dayjs'; @Service() export default class SubscriptionService { @@ -371,11 +372,10 @@ export default class SubscriptionService { const err = await this.killTask(command); const absolutePath = await this.handleLogPath(doc.log_path as string); const str = err ? `\n${err}` : ''; + fs.appendFileSync( `${absolutePath}`, - `${str}\n## 执行结束... ${new Date() - .toLocaleString('zh', { hour12: false }) - .replace(' 24:', ' 00:')} `, + `${str}\n## 执行结束... ${dayjs().format('YYYY-MM-DD HH:mm:ss')} `, ); } diff --git a/back/services/user.ts b/back/services/user.ts index aa260e46..5d912b75 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -13,7 +13,7 @@ import { Request } from 'express'; import ScheduleService from './schedule'; import { spawn } from 'child_process'; import SockService from './sock'; -import got from 'got'; +import dayjs from 'dayjs'; @Service() export default class UserService { @@ -111,7 +111,7 @@ export default class UserService { }); await this.notificationService.notify( '登录通知', - `你于${new Date(timestamp).toLocaleString()}在 ${address} ${ + `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${ req.platform }端 登录成功,ip地址 ${ip}`, ); @@ -140,7 +140,7 @@ export default class UserService { }); await this.notificationService.notify( '登录通知', - `你于${new Date(timestamp).toLocaleString()}在 ${address} ${ + `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${ req.platform }端 登录失败,ip地址 ${ip}`, );