mirror of https://github.com/openspug/spug
A 任务计划添加Corn触发器支持
parent
9045235e41
commit
9aa904c753
|
@ -40,6 +40,8 @@ class Task(models.Model, ModelMixin):
|
|||
tmp = super().to_dict(*args, **kwargs)
|
||||
tmp['targets'] = json.loads(self.targets)
|
||||
tmp['latest_status_alias'] = self.get_latest_status_display()
|
||||
if self.trigger == 'cron':
|
||||
tmp['trigger_args'] = json.loads(self.trigger_args)
|
||||
return tmp
|
||||
|
||||
def __repr__(self):
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.interval import IntervalTrigger
|
||||
from apscheduler.triggers.date import DateTrigger
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
from apscheduler.events import EVENT_SCHEDULER_SHUTDOWN, EVENT_JOB_MAX_INSTANCES, EVENT_JOB_ERROR, EVENT_JOB_EXECUTED
|
||||
from django_redis import get_redis_connection
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
|
@ -37,6 +38,11 @@ class Scheduler:
|
|||
return IntervalTrigger(seconds=int(trigger_args), timezone=cls.timezone)
|
||||
elif trigger == 'date':
|
||||
return DateTrigger(run_date=trigger_args, timezone=cls.timezone)
|
||||
elif trigger == 'cron':
|
||||
args = json.loads(trigger_args) if not isinstance(trigger_args, dict) else trigger_args
|
||||
minute, hour, day, month, week = args['rule'].split()
|
||||
return CronTrigger(minute=minute, hour=hour, day=day, month=month, week=week, start_date=args['start'],
|
||||
end_date=args['stop'])
|
||||
else:
|
||||
raise TypeError(f'unknown schedule policy: {trigger!r}')
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import store from './store';
|
|||
import hostStore from '../host/store';
|
||||
import styles from './index.module.css';
|
||||
import moment from 'moment';
|
||||
import lds from 'lodash';
|
||||
|
||||
@observer
|
||||
class ComForm extends React.Component {
|
||||
|
@ -39,6 +40,13 @@ class ComForm extends React.Component {
|
|||
switch (trigger) {
|
||||
case 'date':
|
||||
return this.state.args['date'].format('YYYY-MM-DD HH:mm:ss');
|
||||
case 'cron':
|
||||
const {rule, start, stop} = this.state.args['cron'];
|
||||
return JSON.stringify({
|
||||
rule,
|
||||
start: start ? start.format('YYYY-MM-DD HH:mm:ss') : null,
|
||||
stop: stop ? stop.format('YYYY-MM-DD HH:mm:ss') : null
|
||||
});
|
||||
default:
|
||||
return this.state.args[trigger];
|
||||
}
|
||||
|
@ -89,6 +97,12 @@ class ComForm extends React.Component {
|
|||
this.setState({args})
|
||||
};
|
||||
|
||||
handleCronArgs = (key, value) => {
|
||||
let args = this.state.args['cron'] || {};
|
||||
args = Object.assign(args, {[key]: value});
|
||||
this.setState({args: Object.assign(this.state.args, {cron: args})})
|
||||
};
|
||||
|
||||
verifyButtonStatus = () => {
|
||||
const data = this.props.form.getFieldsValue();
|
||||
let b1 = data['type'] && data['name'] && this.state.command;
|
||||
|
@ -210,7 +224,29 @@ class ComForm extends React.Component {
|
|||
onChange={v => this.handleArgs('date', v)}/>
|
||||
</Form.Item>
|
||||
</Tabs.TabPane>
|
||||
<Tabs.TabPane disabled tab="UNIX Cron" key="cron">
|
||||
<Tabs.TabPane tab="UNIX Cron" key="cron">
|
||||
<Form.Item required label="执行规则" help="兼容Cron风格,可参考官方例子">
|
||||
<Input
|
||||
value={lds.get(args, 'cron.rule')}
|
||||
placeholder="例如每天凌晨1点执行:0 1 * * *"
|
||||
onChange={e => this.handleCronArgs('rule', e.target.value)}/>
|
||||
</Form.Item>
|
||||
<Form.Item label="生效时间" help="定义的执行规则在到达该时间后生效">
|
||||
<DatePicker
|
||||
showTime
|
||||
style={{width: '100%'}}
|
||||
placeholder="可选输入"
|
||||
value={lds.get(args, 'cron.start') ? moment(args['cron']['start']) : undefined}
|
||||
onChange={v => this.handleCronArgs('start', v)}/>
|
||||
</Form.Item>
|
||||
<Form.Item label="结束时间" help="执行规则在到达该时间后不再执行">
|
||||
<DatePicker
|
||||
showTime
|
||||
style={{width: '100%'}}
|
||||
placeholder="可选输入"
|
||||
value={lds.get(args, 'cron.stop') ? moment(args['cron']['stop']) : undefined}
|
||||
onChange={v => this.handleCronArgs('stop', v)}/>
|
||||
</Form.Item>
|
||||
</Tabs.TabPane>
|
||||
<Tabs.TabPane disabled tab="日历间隔" key="calendarinterval">
|
||||
</Tabs.TabPane>
|
||||
|
|
Loading…
Reference in New Issue