Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve Cron schedule support #1395

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"clean-webpack-plugin": "3.0.0",
"clsx": "^1.2.1",
"codemirror": "^5.64.0",
"cronstrue": "2.47.0",
"core-decorators": "0.20.0",
"core-js": "^3.19.1",
"cron-parser": "^4.7.0",
Expand Down
78 changes: 77 additions & 1 deletion querybook/webapp/__tests__/lib/utils/cron.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { cronToRecurrence, recurrenceToCron } from 'lib/utils/cron';
import {
cronToRecurrence,
recurrenceToCron,
validateCronForRecurrrence,
} from 'lib/utils/cron';

test('basic recurrence to cron', () => {
expect(
Expand Down Expand Up @@ -67,6 +71,7 @@ test('basic cron to recurrence', () => {

recurrence: 'daily',
on: {},
cron: '1 1 * * *',
});
});

Expand All @@ -77,6 +82,7 @@ test('yearly cron to recurrence', () => {

recurrence: 'yearly',
on: { dayMonth: [1, 2, 3], month: [3, 6, 9, 12] },
cron: '1 1 1,2,3 3,6,9,12 *',
});
});

Expand All @@ -87,6 +93,7 @@ test('monthly cron to recurrence', () => {

recurrence: 'monthly',
on: { dayMonth: [1, 2, 3] },
cron: '1 1 1,2,3 * *',
});
});

Expand All @@ -97,6 +104,7 @@ test('weekly cron to recurrence', () => {

recurrence: 'weekly',
on: { dayWeek: [1, 2, 3] },
cron: '1 1 * * 1,2,3',
});
});

Expand All @@ -107,5 +115,73 @@ test('hourly cron to recurrence', () => {

recurrence: 'hourly',
on: {},
cron: '1 * * * *',
});
});

test('every 5 minutes cron to recurrence', () => {
expect(cronToRecurrence('*/5 * * * *')).toStrictEqual({
hour: 0,
minute: 0,

recurrence: 'cron',
on: {},
cron: '*/5 * * * *',
});
});

test('at 22:00 on every day-of-week from Monday through Friday cron to recurrence', () => {
expect(cronToRecurrence('0 22 * * 1-5')).toStrictEqual({
hour: 0,
minute: 0,

recurrence: 'cron',
on: {},
cron: '0 22 * * 1-5',
});
});

test('at minute 0 past hour 0 and 12 on day-of-month 1 and 15 in January and July cron to recurrence', () => {
expect(cronToRecurrence('0 0,12 1,15 1,7 *')).toStrictEqual({
hour: 0,
minute: 0,

recurrence: 'cron',
on: {},
cron: '0 0,12 1,15 1,7 *',
});
});

test('validate hourly cron', () => {
expect(validateCronForRecurrrence('0 * * * *')).toBe(true);
});
test('validate daily cron', () => {
expect(validateCronForRecurrrence('1 1 * * *')).toBe(true);
});
test('validate weekly cron', () => {
expect(validateCronForRecurrrence('0 0 * * 1,3,5')).toBe(true);
});
test('validate monthly cron', () => {
expect(validateCronForRecurrrence('0 0 1,15 1,7 *')).toBe(true);
});
test('validate cron with step', () => {
expect(validateCronForRecurrrence('*/30 * * * *')).toBe(false);
});
test('validate cron with range', () => {
expect(validateCronForRecurrrence('0 22 * * 1-5')).toBe(false);
});
test('validate cron with minute and hour lists', () => {
expect(validateCronForRecurrrence('30 0,1,2 * * *')).toBe(false);
});
test('validate cron with minute list', () => {
expect(validateCronForRecurrrence('0,15,30,45 * * * *')).toBe(false);
});
test('validate cron with monthday and weekday', () => {
expect(validateCronForRecurrrence('0 0 1,15 * 1,7')).toBe(false);
});
test('validate cron with missing minute', () => {
expect(validateCronForRecurrrence('* 0 * * *')).toBe(false);
});
test('validate cron with missing minute and hour', () => {
expect(validateCronForRecurrrence('* * * * *')).toBe(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export const DataDocScheduleForm: React.FunctionComponent<
<RecurrenceEditor
recurrence={values.recurrence}
recurrenceError={errors?.recurrence}
allowCron={false}
setRecurrence={(val) =>
setFieldValue('recurrence', val)
}
Expand Down
19 changes: 19 additions & 0 deletions querybook/webapp/components/DescribeCron/DescribeCron.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cronstrue from 'cronstrue';
import React, { useMemo } from 'react';

export const DescribeCron: React.FunctionComponent<{ cron?: string }> = ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't feel necessary to have this as a separate component. could be just a useMemo inside the RecurrenceEditor component.

cron,
}) => {
const description = useMemo(() => {
try {
if (cron) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: when it is (!cron), it will also raise exception. can just leave it to the try/catch to handle

return cronstrue.toString(cron);
}
} catch (e) {
return null;
}
return null;
}, [cron]);

return <>{description}</>;
};
99 changes: 19 additions & 80 deletions querybook/webapp/components/Task/TaskEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
recurrenceOnYup,
recurrenceToCron,
recurrenceTypes,
validateCronForRecurrrence,
} from 'lib/utils/cron';
import { TaskScheduleResource } from 'resource/taskSchedule';
import { AsyncButton } from 'ui/AsyncButton/AsyncButton';
Expand All @@ -26,7 +25,6 @@ import { RecurrenceEditor } from 'ui/ReccurenceEditor/RecurrenceEditor';
import { Tabs } from 'ui/Tabs/Tabs';
import { TimeFromNow } from 'ui/Timer/TimeFromNow';
import { Title } from 'ui/Title/Title';
import { ToggleButton } from 'ui/ToggleButton/ToggleButton';

import './TaskEditor.scss';

Expand All @@ -42,20 +40,14 @@ interface IProps {
const taskFormSchema = Yup.object().shape({
name: Yup.string().required(),
task: Yup.string().required(),
isCron: Yup.boolean(),
recurrence: Yup.object().when('isCron', (isCron, schema) =>
!isCron
? schema.shape({
hour: Yup.number().min(0).max(23),
minute: Yup.number().min(0).max(59),
recurrence: Yup.string().oneOf(recurrenceTypes),
on: recurrenceOnYup,
})
: schema
),
cron: Yup.string().when('isCron', (isCron, schema) =>
isCron ? schema.required() : schema
),

recurrence: Yup.object().shape({
hour: Yup.number().min(0).max(23),
minute: Yup.number().min(0).max(59),
recurrence: Yup.string().oneOf(recurrenceTypes),
on: recurrenceOnYup,
cron: Yup.string().optional(),
}),
enabled: Yup.boolean().required(),
arg: Yup.array().of(Yup.mixed()),
kwargs: Yup.array().of(Yup.mixed()),
Expand Down Expand Up @@ -100,9 +92,7 @@ export const TaskEditor: React.FunctionComponent<IProps> = ({

const handleTaskEditSubmit = React.useCallback(
(editedValues) => {
const editedCron = editedValues.isCron
? editedValues.cron
: recurrenceToCron(editedValues.recurrence);
const editedCron = recurrenceToCron(editedValues.recurrence);
const editedArgs = editedValues.args
.filter((arg) => !(arg === ''))
.map(stringToTypedVal);
Expand Down Expand Up @@ -173,9 +163,7 @@ export const TaskEditor: React.FunctionComponent<IProps> = ({
return {
name: task.name || '',
task: task.task || '',
isCron: !validateCronForRecurrrence(cron),
recurrence,
cron,
enabled: task.enabled ?? true,
args: task.args || [],
kwargs: Object.entries(task.kwargs || {}),
Expand Down Expand Up @@ -299,8 +287,6 @@ export const TaskEditor: React.FunctionComponent<IProps> = ({
</div>
);

const canUseRecurrence = validateCronForRecurrrence(values.cron);

return (
<div className="TaskEditor-form">
<FormWrapper minLabelWidth="180px" size={7}>
Expand Down Expand Up @@ -349,63 +335,16 @@ export const TaskEditor: React.FunctionComponent<IProps> = ({
/>
</div>
{values.enabled ? (
<div className="TaskEditor-schedule horizontal-space-between">
{values.isCron || !canUseRecurrence ? (
<SimpleField
label="Cron Schedule"
type="input"
name="cron"
/>
) : (
<FormField stacked label="Schedule">
<RecurrenceEditor
recurrence={values.recurrence}
recurrenceError={
errors?.recurrence
}
setRecurrence={(val) =>
setFieldValue(
'recurrence',
val
)
}
/>
</FormField>
)}
{canUseRecurrence ? (
<div className="TaskEditor-schedule-toggle mr16">
<ToggleButton
checked={values.isCron}
onClick={(val: boolean) => {
setFieldValue(
'isCron',
val
);
if (val) {
setFieldValue(
'cron',
recurrenceToCron(
values.recurrence
)
);
} else {
setFieldValue(
'recurrence',
cronToRecurrence(
values.cron
)
);
}
}}
title={
values.isCron
? 'Use Recurrence Editor'
: 'Use Cron'
}
/>
</div>
) : null}
</div>
<FormField stacked label="Schedule">
<RecurrenceEditor
recurrence={values.recurrence}
recurrenceError={errors?.recurrence}
setRecurrence={(val) =>
setFieldValue('recurrence', val)
}
allowCron={true}
/>
</FormField>
) : null}
</div>
<div className="TaskEditor-form-controls right-align mt16">
Expand Down
Loading
Loading