Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function mapCronToHourFormat(cronExpression) {
try {
cronstrue.toString(cronExpression);
} catch (_) {
return null;
}
const cronParts = cronExpression.split(' ');
if (cronParts.length === 6) {
// disregard seconds component
cronParts.shift();
}
const [cronMinute, cronHour, cronDayOfMonth, cronMonth, cronDaysOfWeek] = cronParts;
const time = {};
time.minute = cronMinute.includes('*') ? 0 : Number(cronMinute);
time.date = cronDayOfMonth.includes('*') ? 1 : Number(cronDayOfMonth);
time.month = cronMonth.includes('*') ? 1 : Number(cronMonth);
time.days = cronDaysOfWeek.includes('*') ? ['MON'] : cronDaysOfWeek.split(',');
getData: ({status, spec, metadata }) => {
return [
['Created:', `${metadata.creationTimestamp} ( ${toHumanizedAge(metadata.creationTimestamp)} ago )`],
['Schedule:', `${spec.schedule} ( ${cronstrue.toString(spec.schedule)} )`],
['Concurrency Policy:', spec.concurrencyPolicy],
['Suspend', spec.suspend],
// ['Starting Deadline Seconds:', spec.startingDeadlineSeconds || ''],
['Selector:', (spec.selector && spec.selector.matchLabels) || ''],
['Parallelism:', spec.parallelism || ''],
['Last Schedule Time:', status.lastScheduleTime],
//Completions: // => need to get details of owned Jobs for this...
]
},
},
export function validateRollupCron(rollupCron) {
if (!rollupCron || !rollupCron.trim()) {
return [
,
];
}
try {
cronstrue.toString(rollupCron);
} catch (error) {
const prefix = 'Error: ';
const prefixIndex = error.indexOf(prefix);
// Note: cronstrue ships with a localizable version. Refer to the docs for more
// info on how we can display a localized error value.
return error.substring(prefixIndex + prefix.length);
}
return undefined;
}
function getCronReadableDesription(cronStr: string): string[] | undefined {
recordPageView('/getCronReadableDesription');
let cron: any | undefined;
try {
cron = cronParser.parseExpression(cronStr, { utc: true });
} catch (ex) { // 此处是故意 ignore。只有当用户表达式书写有问题时会 throw error
return;
}
let readableDescription: string | undefined;
let sysLocale = osLocale.sync() || 'en';
const timeZone = jstz.determine().name() || 'Asia/Shanghai';
readableDescription = cronstrue.toString(cronStr, { locale: sysLocale.replace('-', '_') });
const prevTime = new Date(cron.prev().toISOString());
cron.reset();
const nextTime = new Date(cron.next().toISOString());
const result = [
'__CronExpression Description(UTC)__\n',
`\t${readableDescription}\n`,
'__Last execution time__\n',
`\tUTC: ${prevTime.toLocaleString(sysLocale, { timeZone: 'UTC' })}\n`,
`\t${timeZone}: ${prevTime.toLocaleString(sysLocale, { timeZone: timeZone })}\n`,
'__Next execution time__\n',
`\tUTC: ${nextTime.toLocaleString(sysLocale, { timeZone: 'UTC' })}\n`,
`\t${timeZone}: ${nextTime.toLocaleString(sysLocale, { timeZone: timeZone })}`
];
return result;
}
preview: function() {
return this.manual ? 'Imports will run manually' : cronstrue.toString(this.expression)
}
},
export function getReadableCronDescriptor(expression) {
return cronstrue.toString(expression);
}
export const getTimeFromCronExpr = (cronValue) => {
let result;
try {
result = cronstrue.toString(cronValue)
} catch (err) {
}
return result || ''
};
const getNaturalLanguageCronString = (cronSchedule: string) => {
try {
return cronstrue.toString(cronSchedule);
} catch {
return "Invalid cron string";
}
};
Object.entries(requirements[`procs-${name}`]).forEach(([proc, ext]) => {
if (ext.type === 'cron') {
res.push([name, proc, `taskcluster.${name}`, ext.deadline, cronstrue.toString(ext.schedule)]);
}
if (ext.type === 'background' && ext.subType === 'iterate') {
res.push([name, proc, `taskcluster.${name}`, 'continuous', 'continuous']);
}
});
function cronValidator (value, allState) {
if (isRequired(value) && !allState.run_immediately) {
return 'Required if run immediately is unchecked'
}
if(value){
try {
cronstrue.toString(value)
} catch (err) {
return 'illegal cron input'
}
}
}
function emailsValidator () {