From a6abc1512672e9d21a2e989d566b499385f529ea Mon Sep 17 00:00:00 2001 From: whyour Date: Tue, 6 Apr 2021 17:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=90=E8=A1=8C=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=90=8E=E6=97=A5=E5=BF=97=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 20 ++++++++++++ src/pages/crontab/index.tsx | 47 ++++++++++++++++------------ src/pages/crontab/logModal.tsx | 56 ++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 src/pages/crontab/logModal.tsx diff --git a/back/api/cron.ts b/back/api/cron.ts index 2567cc24..2c7bee03 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -185,4 +185,24 @@ export default (app: Router) => { } }, ); + + route.get( + '/crons/:id', + celebrate({ + params: Joi.object({ + id: Joi.string().required(), + }), + }), + async (req: Request, res: Response, next: NextFunction) => { + const logger: Logger = Container.get('logger'); + try { + const cookieService = Container.get(CronService); + const data = await cookieService.get(req.params.id); + return res.send({ code: 200, data }); + } catch (e) { + logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); }; diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 8be6c394..ee19603b 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -28,6 +28,7 @@ import config from '@/utils/config'; import { PageContainer } from '@ant-design/pro-layout'; import { request } from '@/utils/http'; import CronModal from './modal'; +import CronLogModal from './logModal'; const { Text } = Typography; const { Search } = Input; @@ -119,7 +120,8 @@ const Crontab = () => { { - logCron(record); + setLogCron(record); + setIsLogModalVisible(true); }} > @@ -139,6 +141,8 @@ const Crontab = () => { const [isModalVisible, setIsModalVisible] = useState(false); const [editedCron, setEditedCron] = useState(); const [searchText, setSearchText] = useState(''); + const [isLogModalVisible, setIsLogModalVisible] = useState(false); + const [logCron, setLogCron] = useState(); const getCrons = () => { setLoading(true); @@ -210,7 +214,6 @@ const Crontab = () => { ...record, status: CrontabStatus.running, }); - console.log(result); setValue(result); } else { notification.error({ @@ -276,23 +279,6 @@ const Crontab = () => { }); }; - const logCron = (record: any) => { - request - .get(`${config.apiPrefix}crons/${record._id}/log`) - .then((data: any) => { - Modal.info({ - width: 650, - title: `${record.name || record._id}`, - content: ( -
-              {data.data || '暂无日志'}
-            
- ), - onOk() {}, - }); - }); - }; - const MoreBtn: React.FC<{ record: any; index: number; @@ -360,6 +346,21 @@ const Crontab = () => { setSearchText(value); }; + const getCronDetail = () => { + request + .get(`${config.apiPrefix}crons/${logCron._id}`) + .then((data: any) => { + const index = value.findIndex((x) => x._id === logCron._id); + const result = [...value]; + result.splice(index, 1, { + ...logCron, + status: data.data.status, + }); + setValue(result); + }) + .finally(() => setLoading(false)); + }; + useEffect(() => { getCrons(); }, [searchText]); @@ -419,6 +420,14 @@ const Crontab = () => { bordered scroll={{ x: 768 }} /> + { + getCronDetail(); + setIsLogModalVisible(false); + }} + cron={logCron} + /> void; +}) => { + const [value, setValue] = useState('运行中...'); + const [logTimer, setLogTimer] = useState(); + + const getCronLog = () => { + request + .get(`${config.apiPrefix}crons/${cron._id}/log`) + .then((data: any) => { + setValue(data.data); + }); + }; + + const cancel = () => { + clearInterval(logTimer); + handleCancel(); + }; + + useEffect(() => { + if (cron) { + const timer = setInterval(() => { + getCronLog(); + }, 2000); + setLogTimer(timer); + } + }, [cron]); + + return ( + cancel()} + onCancel={() => cancel()} + destroyOnClose + > +
+        {value}
+      
+
+ ); +}; + +export default CronLogModal;