Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function button(label: string) {
describe("scheduled task batched edits", () => {
it("submits the selected interval when saved before React commits", async () => {
await act(async () => {
button("Interval").click();
button("Select Interval").click();
button("Save changes").click();
});
expect(mocks.alert).not.toHaveBeenCalled();
Expand All @@ -219,7 +219,7 @@ describe("scheduled task batched edits", () => {
});
it("retains the interval when enabled changes before React commits", async () => {
act(() => {
button("Interval").click();
button("Select Interval").click();
button("Toggle enabled").click();
});
expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current?.draft.schedule.mode).toBe(
Expand All @@ -238,7 +238,7 @@ describe("scheduled task batched edits", () => {
});
});
it("keeps every webhook filter added before React commits", () => {
act(() => button("On webhook").click());
act(() => button("Select On webhook").click());
act(() => {
button("Add filter").click();
button("Add filter").click();
Expand All @@ -253,7 +253,7 @@ describe("scheduled task batched edits", () => {
act(() => startScheduledTaskEditor({ ...editor, draft: { ...editor.draft, task: null } }));
act(() => {
button("Select Target").click();
button("Interval").click();
button("Select Interval").click();
button("Select Project").click();
});
expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current).toMatchObject({
Expand Down
165 changes: 121 additions & 44 deletions apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
MAX_WEBHOOK_DELIVERY_AGE_MINUTES,
MAX_WEBHOOK_FILTERS,
resolveEnvironmentMachineKind,
scheduledTaskCronError,
scheduledTaskThreadMode,
} from "@t3tools/contracts";
import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail";
Expand Down Expand Up @@ -57,7 +58,6 @@ import {
import { Menu } from "../../components/ui/Menu";
import type { ComposerEditorSelection } from "../../components/ComposerEditor";
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
import { SegmentedControl } from "../../components/ui/SegmentedControl";
import { ThemedSwitch } from "../../components/ui/ThemedSwitch";
import { webhookAddress } from "@t3tools/client-runtime/webhook-address";
import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic";
Expand All @@ -77,6 +77,7 @@ import { SettingsSection } from "./components/SettingsSection";
import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-environment-filter";
import {
changeDraftProject,
DEFAULT_CRON_EXPRESSION,
editDraft,
parseIntervalMinutes,
scheduledTaskDefaultModel,
Expand Down Expand Up @@ -137,10 +138,16 @@ function describeSchedule(
}
if (task.schedule.type === "interval")
return formatScheduledTaskInterval(task.schedule.everyMs, resolve);
const zone = scheduleTimeZoneLabel(timeZone, task.nextRunAt);
if (task.schedule.type === "cron") {
const expression = task.schedule.expression;
return zone === undefined
? resolve(msg("Cron {expression}", { expression }))
: resolve(msg("Cron {expression} {zone}", { expression, zone }));
}
const days = task.schedule.weekdays?.length
? repeatLabel(task.schedule.weekdays, resolve, locale)
: resolve(msg("Every day"));
const zone = scheduleTimeZoneLabel(timeZone, task.nextRunAt);
const time = formatTime(task.schedule.timeOfDay);
return zone === undefined
? resolve(msg("{days} at {time}", { days, time }))
Expand Down Expand Up @@ -203,6 +210,9 @@ function FormField(props: {
readonly disabled?: boolean;
readonly placeholder?: string;
readonly borderTop?: boolean;
/** For code-like values that capitalization and autocorrect would change. */
readonly verbatim?: boolean;
readonly hint?: string;
}) {
return (
<View
Expand All @@ -213,13 +223,15 @@ function FormField(props: {
<Text className="text-sm text-foreground-muted">{props.label}</Text>
<RNTextInput
accessibilityLabel={props.label}
accessibilityHint={props.hint}
value={props.value}
readOnly={props.disabled}
onChangeText={props.onChange}
textAlignVertical="center"
keyboardType={props.keyboardType}
placeholder={props.placeholder}
placeholderTextColorClassName="accent-foreground-muted"
{...(props.verbatim ? { autoCapitalize: "none", autoCorrect: false } : {})}
className="min-h-8 font-sans text-base text-foreground"
/>
</View>
Expand Down Expand Up @@ -775,6 +787,25 @@ function TaskForm({
const offersOnce =
config?.environment.capabilities.oneTimeSchedules === true ||
draft.task?.schedule.type === "once";
// Older servers reject cron schedules on save.
const offersCron =
config?.environment.capabilities.cronSchedules === true || draft.task?.schedule.type === "cron";
const cronError =
draft.schedule.mode === "cron" ? scheduledTaskCronError(draft.schedule.cronExpression) : null;
const scheduleModes = [
{ value: "fixed_time" as const, label: gt("At a time") },
// Older servers reject one-time schedules on save.
...(offersOnce || draft.schedule.mode === "once"
? [{ value: "once" as const, label: gt("Once", { $context: "one-time schedule option" }) }]
: []),
{ value: "interval" as const, label: gt("Interval") },
...(offersCron || draft.schedule.mode === "cron"
? [{ value: "cron" as const, label: gt("Cron") }]
: []),
...(webhookDenial === undefined || draft.schedule.mode === "webhook"
? [{ value: "webhook" as const, label: gt("On webhook") }]
: []),
];
const offersThreadModes = config?.environment.capabilities.scheduledTaskThreadModes === true;
const save = async () => {
const current = readEditor();
Expand Down Expand Up @@ -818,6 +849,19 @@ function TaskForm({
);
return;
}
if (draft.schedule.mode === "cron" && !offersCron) {
Dialog.alert(
gt("Cron is not available here"),
gt(
"This environment runs an older T3 Code. Update it to schedule a cron task, or pick another schedule.",
),
);
return;
}
if (cronError !== null) {
Dialog.alert(gt("Invalid cron expression"), cronError);
return;
}
const parsedFilters = parseWebhookFilterDrafts(draft.schedule.filters, m);
if (draft.schedule.mode === "webhook" && "error" in parsedFilters) {
Dialog.alert(gt("Invalid filter"), parsedFilters.error);
Expand Down Expand Up @@ -1122,47 +1166,38 @@ function TaskForm({
</SettingsSection>

<SettingsSection title={gt("Schedule", { $context: "noun, section heading" })}>
<View className="px-4 py-3">
<SegmentedControl
variant="card"
options={[
{ value: "fixed_time", label: gt("At a time") },
// Older servers reject one-time schedules on save.
...(offersOnce || draft.schedule.mode === "once"
? [
{
value: "once" as const,
label: gt("Once", { $context: "one-time schedule option" }),
},
]
: []),
{ value: "interval", label: gt("Interval") },
...(webhookDenial === undefined || draft.schedule.mode === "webhook"
? [{ value: "webhook" as const, label: gt("On webhook") }]
: []),
]}
selected={draft.schedule.mode}
onSelect={(mode) => {
setTimePickerOpen(false);
setRunAtPicker(null);
setDraft((current) => ({
...current,
prompt:
mode === "webhook" && !current.prompt.trim()
? DEFAULT_WEBHOOK_PROMPT
: current.prompt,
schedule: {
...current.schedule,
mode,
runAt:
mode === "once" && !current.schedule.runAt
? new Date(Math.ceil(Date.now() / 3_600_000) * 3_600_000).toISOString()
: current.schedule.runAt,
},
}));
}}
/>
</View>
<SelectRow
label={gt("Type")}
value={
scheduleModes.find((option) => option.value === draft.schedule.mode)?.label ?? ""
}
actions={scheduleModes.map((option) => ({
id: option.value,
title: option.label,
state: option.value === draft.schedule.mode ? "on" : undefined,
}))}
onSelect={(id) => {
const mode = scheduleModes.find((option) => option.value === id)?.value;
if (mode === undefined) return;
setTimePickerOpen(false);
setRunAtPicker(null);
setDraft((current) => ({
...current,
prompt:
mode === "webhook" && !current.prompt.trim()
? DEFAULT_WEBHOOK_PROMPT
: current.prompt,
schedule: {
...current.schedule,
mode,
runAt:
mode === "once" && !current.schedule.runAt
? new Date(Math.ceil(Date.now() / 3_600_000) * 3_600_000).toISOString()
: current.schedule.runAt,
},
}));
}}
/>
{draft.schedule.mode === "fixed_time" ? (
<>
<Pressable
Expand Down Expand Up @@ -1310,6 +1345,48 @@ function TaskForm({
</View>
);
})
) : draft.schedule.mode === "cron" ? (
<>
<FormField
label={gt("Cron expression")}
value={draft.schedule.cronExpression}
placeholder={DEFAULT_CRON_EXPRESSION}
disabled={saving}
borderTop
verbatim
hint={
cronError === null
? gt(
"Minute, hour, day of month, month, weekday. {example} runs every 30 minutes from 9:00 to 16:30 on weekdays.",
{ example: DEFAULT_CRON_EXPRESSION },
)
: gt("Error: {error}", { error: cronError })
}
onChange={(cronExpression) =>
setDraft((current) => ({
...current,
schedule: { ...current.schedule, cronExpression },
}))
}
/>
<Text
className={
cronError === null
? "px-4 pb-3 text-sm text-foreground-muted"
: "px-4 pb-3 text-sm text-danger-foreground"
}
>
{cronError ??
gt(
"Minute, hour, day of month, month, weekday. {example} runs every 30 minutes from 9:00 to 16:30 on weekdays.",
{
example: [...DEFAULT_CRON_EXPRESSION.replaceAll(" ", "\u00a0")].join(
"\u2060",
),
},
)}
</Text>
</>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) : draft.schedule.mode === "webhook" ? (
<>
<WebhookScheduleDetails
Expand Down Expand Up @@ -1438,7 +1515,7 @@ function TaskForm({
/>
</View>
</SettingsSection>
{draft.schedule.mode === "fixed_time" ? (
{draft.schedule.mode === "fixed_time" || draft.schedule.mode === "cron" ? (
<Text className="px-2 text-sm text-foreground-muted">
<T>Time uses the environment's time zone, which may differ from your phone's.</T>
</Text>
Expand Down
15 changes: 15 additions & 0 deletions apps/mobile/src/features/settings/scheduledTaskDraft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ describe("scheduleDraftForTask", () => {
expect(scheduleFromDraft(draft)).toEqual({ type: "interval", everyMs: 60_000 });
});

it("round-trips a cron expression, sends it trimmed, and refuses one the server would reject", () => {
const draft = scheduleDraftForTask({
schedule: { type: "cron", expression: "*/30 9-16 * * MON-FRI" },
});
expect(draft.mode).toBe("cron");
expect(scheduleFromDraft(draft)).toEqual({ type: "cron", expression: "*/30 9-16 * * MON-FRI" });
expect(scheduleFromDraft({ ...draft, cronExpression: " 0 9 * * 1 " })).toEqual({
type: "cron",
expression: "0 9 * * 1",
});
for (const cronExpression of ["* * * * * *", "0 25 * * *", "0 9 30 2 *"]) {
expect(scheduleFromDraft({ ...draft, cronExpression })).toBeNull();
}
});

it("round-trips dedupe settings and rejects invalid windows", () => {
const dedupe = {
replayKey: "{{headers.delivery-id}}",
Expand Down
22 changes: 20 additions & 2 deletions apps/mobile/src/features/settings/scheduledTaskDraft.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { isWebhookDedupeTemplate, scheduledTaskThreadMode } from "@t3tools/contracts";
import {
isWebhookDedupeTemplate,
scheduledTaskCronError,
scheduledTaskThreadMode,
} from "@t3tools/contracts";
import type {
ModelSelection,
ServerConfig,
Expand Down Expand Up @@ -52,11 +56,16 @@ export function scheduledTaskDefaultModel(
});
}

/** Weekdays every 30 minutes from 09:00 to 16:30, a starting point for the cron field. */
export const DEFAULT_CRON_EXPRESSION = "*/30 9-16 * * 1-5";

export type ScheduleDraft = {
readonly mode: "fixed_time" | "once" | "interval" | "webhook";
readonly mode: "fixed_time" | "once" | "interval" | "cron" | "webhook";
readonly timeOfDay: string;
/** ISO time for a one-time run. Empty until the user picks the "Once" mode. */
readonly runAt: string;
/** Five-field cron, read in the environment's time zone. */
readonly cronExpression: string;
readonly weekdays: ReadonlyArray<number>;
readonly intervalMinutes: string;
readonly signature: ScheduledTaskWebhookSignature | null;
Expand All @@ -73,6 +82,7 @@ export const DEFAULT_SCHEDULE: ScheduleDraft = {
mode: "fixed_time",
timeOfDay: "09:00",
runAt: "",
cronExpression: DEFAULT_CRON_EXPRESSION,
weekdays: [1, 2, 3, 4, 5],
intervalMinutes: "15",
signature: null,
Expand All @@ -96,6 +106,8 @@ export function scheduleDraftForTask(task: Pick<ScheduledTask, "schedule">): Sch
};
case "once":
return { ...DEFAULT_SCHEDULE, mode: "once", runAt: task.schedule.runAt };
case "cron":
return { ...DEFAULT_SCHEDULE, mode: "cron", cronExpression: task.schedule.expression };
case "interval":
return {
...DEFAULT_SCHEDULE,
Expand Down Expand Up @@ -177,6 +189,11 @@ export function scheduleFromDraft(draft: ScheduleDraft): ScheduledTaskUpsertSche
const runAt = new Date(draft.runAt);
return Number.isNaN(runAt.getTime()) ? null : { type: "once", runAt: runAt.toISOString() };
}
if (draft.mode === "cron") {
return scheduledTaskCronError(draft.cronExpression) === null
? { type: "cron", expression: draft.cronExpression.trim() }
: null;
}
if (draft.mode === "interval") {
const minutes = parseIntervalMinutes(draft.intervalMinutes);
const everyMs = Math.round(minutes * 60_000);
Expand Down Expand Up @@ -230,6 +247,7 @@ function draftSignature(draft: ScheduledTaskDraft): string {
draft.schedule.mode,
draft.schedule.timeOfDay,
draft.schedule.runAt,
draft.schedule.cronExpression,
[...draft.schedule.weekdays].sort((a, b) => a - b),
draft.schedule.intervalMinutes,
draft.schedule.maxDeliveryAgeMinutes,
Expand Down
Loading
Loading