Skip to content

Commit

Permalink
修复定时任务间隔较小,任务状态不准确
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Sep 26, 2023
1 parent 77a8e00 commit eddc03e
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
5 changes: 2 additions & 3 deletions back/api/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,12 @@ export default (app: Router) => {
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.status({
...req.body,
status: parseInt(req.body.status),
pid: parseInt(req.body.pid) || '',
status: req.body.status ? parseInt(req.body.status) : undefined,
pid: req.body.pid ? parseInt(req.body.pid) : undefined,
});
return res.send({ code: 200, data });
} catch (e) {
Expand Down
12 changes: 10 additions & 2 deletions back/services/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import cronClient from '../schedule/client';
import taskLimit from '../shared/pLimit';
import { spawn } from 'cross-spawn';
import dayjs from 'dayjs';
import pickBy from 'lodash/pickBy';
import omit from 'lodash/omit';

@Service()
export default class CronService {
Expand Down Expand Up @@ -89,7 +91,7 @@ export default class CronService {
last_running_time: number;
last_execution_time: number;
}) {
const options: any = {
let options: any = {
status,
pid,
log_path,
Expand All @@ -99,7 +101,13 @@ export default class CronService {
options.last_running_time = last_running_time;
}

return await CrontabModel.update({ ...options }, { where: { id: ids } });
for (const id of ids) {
const cron = await this.getDb({ id });
if (status === CrontabStatus.idle && log_path !== cron.log_path) {
options = omit(options, ['status', 'log_path', 'pid']);
}
await CrontabModel.update({ ...pickBy(options, (v) => v === 0 || !!v) }, { where: { id } });
}
}

public async remove(ids: number[]) {
Expand Down
2 changes: 1 addition & 1 deletion shell/share.sh
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ handle_task_after() {

[[ "$diff_time" == 0 ]] && diff_time=1

echo -e "\n\n## 执行结束... $end_time 耗时 $diff_time 秒     "
echo -e "\n## 执行结束... $end_time 耗时 $diff_time 秒     "

[[ $ID ]] && update_cron "\"$ID\"" "1" "" "$log_path" "$begin_timestamp" "$diff_time"
}
Expand Down
5 changes: 3 additions & 2 deletions src/layouts/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ body {
}

.ant-modal-body {
max-height: calc(90vh - 110px);
max-height: calc(90vh - var(--vh-offset, 110px));
max-height: calc(80vh - 110px);
max-height: calc(80vh - var(--vh-offset, 110px));
overflow-y: auto;
}

Expand Down Expand Up @@ -181,6 +181,7 @@ body {
.react-codemirror2,
.CodeMirror {
height: 100%;
overflow: auto;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/crontab/logModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { PageLoading } from '@ant-design/pro-layout';
import { logEnded } from '@/utils';
import { CrontabStatus } from './type';
import Ansi from 'ansi-to-react';

const { Countdown } = Statistic;

Expand Down Expand Up @@ -148,7 +149,7 @@ const CronLogModal = ({
: {}
}
>
{value}
<Ansi>{value}</Ansi>
</pre>
)}
<div id="log-flag"></div>
Expand Down
5 changes: 3 additions & 2 deletions src/pages/script/editModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SettingModal from './setting';
import { useTheme } from '@/utils/hooks';
import { getEditorMode, logEnded } from '@/utils';
import WebSocketManager from '@/utils/websocket';
import Ansi from 'ansi-to-react';

const { Option } = Select;

Expand Down Expand Up @@ -116,7 +117,7 @@ const EditModal = ({
}, 300);
}

setLog(p=>`${p}${_message}`);
setLog((p) => `${p}${_message}`);
}, []);

useEffect(() => {
Expand Down Expand Up @@ -246,7 +247,7 @@ const EditModal = ({
padding: '0 15px',
}}
>
{log}
<Ansi>{log}</Ansi>
</pre>
</SplitPane>
<SaveModal
Expand Down
4 changes: 2 additions & 2 deletions src/pages/setting/checkUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const CheckUpdate = ({ systemInfo }: any) => {
</div>
</>
),
content: <pre>{lastLog}</pre>,
content: <pre><Ansi>{lastLog}</Ansi></pre>,
okText: intl.get('下载更新'),
cancelText: intl.get('以后再说'),
onOk() {
Expand All @@ -102,7 +102,7 @@ const CheckUpdate = ({ systemInfo }: any) => {
okButtonProps: { disabled: true },
title: intl.get('下载更新中...'),
centered: true,
content: <pre>{value}</pre>,
content: <pre><Ansi>{value}</Ansi></pre>,
});
};

Expand Down
3 changes: 2 additions & 1 deletion src/pages/subscription/logModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@ant-design/icons';
import { PageLoading } from '@ant-design/pro-layout';
import { logEnded } from '@/utils';
import Ansi from 'ansi-to-react';

const SubscriptionLogModal = ({
subscription,
Expand Down Expand Up @@ -122,7 +123,7 @@ const SubscriptionLogModal = ({
: {}
}
>
{value}
<Ansi>{value}</Ansi>
</pre>
)}
</div>
Expand Down

0 comments on commit eddc03e

Please sign in to comment.