feat: one-time tasks
This commit is contained in:
@@ -6,7 +6,8 @@
|
|||||||
"every_week": "Every week",
|
"every_week": "Every week",
|
||||||
"every_month": "Every month",
|
"every_month": "Every month",
|
||||||
"every_year": "Every year",
|
"every_year": "Every year",
|
||||||
"custom": "Custom (crontab)"
|
"custom": "Custom (crontab)",
|
||||||
|
"one_time": "One time (then delete)"
|
||||||
},
|
},
|
||||||
"week": [
|
"week": [
|
||||||
"Monday",
|
"Monday",
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
"every_week": "Toutes les semaines",
|
"every_week": "Toutes les semaines",
|
||||||
"every_month": "Tous les mois",
|
"every_month": "Tous les mois",
|
||||||
"every_year": "Tous les ans",
|
"every_year": "Tous les ans",
|
||||||
"custom": "Personnalisé (crontab)"
|
"custom": "Personnalisé (crontab)",
|
||||||
|
"one_time": "Une seule fois (puis suppression)"
|
||||||
},
|
},
|
||||||
"week": [
|
"week": [
|
||||||
"Lundi",
|
"Lundi",
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ watch(cron, () => {
|
|||||||
<option :value="CronType.CUSTOM">
|
<option :value="CronType.CUSTOM">
|
||||||
{{ $t("cron.type.custom") }}
|
{{ $t("cron.type.custom") }}
|
||||||
</option>
|
</option>
|
||||||
|
<option :value="CronType.ONE_TIME">
|
||||||
|
{{ $t("cron.type.one_time") }}
|
||||||
|
</option>
|
||||||
</select>
|
</select>
|
||||||
<select
|
<select
|
||||||
v-if="cronType === CronType.EVERY_DAY"
|
v-if="cronType === CronType.EVERY_DAY"
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { isTemporary, taskCompleted, taskNextReset } from "@/lib/tasks";
|
|||||||
import { relativeTime } from "@/lib/dates";
|
import { relativeTime } from "@/lib/dates";
|
||||||
import { useAlertStore } from "@/stores/alerts";
|
import { useAlertStore } from "@/stores/alerts";
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
|
import { isOneTime } from "@/lib/cron";
|
||||||
|
|
||||||
const emit =
|
const emit =
|
||||||
defineEmits<(e: "clone" | "delete" | "updated", task: Task) => void>();
|
defineEmits<(e: "clone" | "delete" | "updated", task: Task) => void>();
|
||||||
@@ -75,20 +76,24 @@ function onSave() {
|
|||||||
function onCheck() {
|
function onCheck() {
|
||||||
if (!isTemporary(task.value)) {
|
if (!isTemporary(task.value)) {
|
||||||
if (!checked.value) {
|
if (!checked.value) {
|
||||||
const data = {
|
if (isOneTime(task.value.reset_cron)) {
|
||||||
check_date: new Date().toISOString(),
|
emit("delete", task.value);
|
||||||
previous_check_date: task.value.check_date?.toISOString(),
|
} else {
|
||||||
};
|
const data = {
|
||||||
task.value.previous_check_date = task.value.check_date;
|
check_date: new Date().toISOString(),
|
||||||
task.value.check_date = new Date();
|
previous_check_date: task.value.check_date?.toISOString(),
|
||||||
updateTask(task.value.id, data)
|
};
|
||||||
.then((data) => {
|
task.value.previous_check_date = task.value.check_date;
|
||||||
task.value = data;
|
task.value.check_date = new Date();
|
||||||
emit("updated", data);
|
updateTask(task.value.id, data)
|
||||||
})
|
.then((data) => {
|
||||||
.catch(() => {
|
task.value = data;
|
||||||
alertError(i18n.t("alerts.task_update_error"));
|
emit("updated", data);
|
||||||
});
|
})
|
||||||
|
.catch(() => {
|
||||||
|
alertError(i18n.t("alerts.task_update_error"));
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const data = {
|
const data = {
|
||||||
check_date: null,
|
check_date: null,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export enum CronType {
|
|||||||
EVERY_MONTH,
|
EVERY_MONTH,
|
||||||
EVERY_YEAR,
|
EVERY_YEAR,
|
||||||
CUSTOM,
|
CUSTOM,
|
||||||
|
ONE_TIME,
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum SortType {
|
export enum SortType {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { CronType } from "@/enums";
|
import { CronType } from "@/enums";
|
||||||
import { CronExpressionParser } from "cron-parser";
|
import { CronExpressionParser } from "cron-parser";
|
||||||
|
|
||||||
|
const ONE_TIME_VALUE = "@one-time";
|
||||||
|
|
||||||
export function parseCron(cron: string | null): {
|
export function parseCron(cron: string | null): {
|
||||||
type: CronType;
|
type: CronType;
|
||||||
value: number;
|
value: number;
|
||||||
@@ -11,6 +13,11 @@ export function parseCron(cron: string | null): {
|
|||||||
type: CronType.MANUAL,
|
type: CronType.MANUAL,
|
||||||
value: 0,
|
value: 0,
|
||||||
};
|
};
|
||||||
|
} else if (cron === ONE_TIME_VALUE) {
|
||||||
|
return {
|
||||||
|
type: CronType.ONE_TIME,
|
||||||
|
value: 0,
|
||||||
|
};
|
||||||
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
||||||
return {
|
return {
|
||||||
type: CronType.EVERY_DAY,
|
type: CronType.EVERY_DAY,
|
||||||
@@ -45,6 +52,8 @@ export function getCron(
|
|||||||
fallback: string,
|
fallback: string,
|
||||||
): string | null {
|
): string | null {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case CronType.ONE_TIME:
|
||||||
|
return ONE_TIME_VALUE;
|
||||||
case CronType.EVERY_DAY:
|
case CronType.EVERY_DAY:
|
||||||
return `0 ${value.toFixed(0)} * * *`;
|
return `0 ${value.toFixed(0)} * * *`;
|
||||||
case CronType.EVERY_WEEK:
|
case CronType.EVERY_WEEK:
|
||||||
@@ -62,6 +71,10 @@ export function getCron(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isOneTime(cron: string | null): boolean {
|
||||||
|
return cron === ONE_TIME_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
export function validCron(cron: string): boolean {
|
export function validCron(cron: string): boolean {
|
||||||
try {
|
try {
|
||||||
CronExpressionParser.parse(cron);
|
CronExpressionParser.parse(cron);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { SortType } from "@/enums";
|
import { SortType } from "@/enums";
|
||||||
import type { RawTask, Task, TaskCreateData } from "@/types";
|
import type { RawTask, Task, TaskCreateData } from "@/types";
|
||||||
import { CronExpressionParser } from "cron-parser";
|
import { CronExpressionParser } from "cron-parser";
|
||||||
|
import { isOneTime } from "@/lib/cron";
|
||||||
|
|
||||||
const TEMPORARY_ID = "tmp";
|
const TEMPORARY_ID = "tmp";
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ export function newTemporaryTask(data: TaskCreateData): Task {
|
|||||||
updated_at: new Date(),
|
updated_at: new Date(),
|
||||||
check_date: null,
|
check_date: null,
|
||||||
previous_check_date: null,
|
previous_check_date: null,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isTemporary(task: Task): boolean {
|
export function isTemporary(task: Task): boolean {
|
||||||
@@ -35,7 +36,11 @@ export function isTemporary(task: Task): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function taskNextReset(task: Task): Date | null {
|
export function taskNextReset(task: Task): Date | null {
|
||||||
if (task.check_date === null || task.reset_cron === null) {
|
if (
|
||||||
|
task.check_date === null ||
|
||||||
|
task.reset_cron === null ||
|
||||||
|
isOneTime(task.reset_cron)
|
||||||
|
) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user