fix crontab task (#19)

This commit is contained in:
paisley
2026-02-09 18:59:21 +08:00
committed by GitHub
Unverified
parent 19ae5b0f75
commit 905b828e9b
2 changed files with 90 additions and 57 deletions

View File

@@ -150,11 +150,11 @@ function transformCronJob(job: GatewayCronJob) {
// Build lastRun from state // Build lastRun from state
const lastRun = job.state?.lastRunAtMs const lastRun = job.state?.lastRunAtMs
? { ? {
time: new Date(job.state.lastRunAtMs).toISOString(), time: new Date(job.state.lastRunAtMs).toISOString(),
success: job.state.lastStatus === 'ok', success: job.state.lastStatus === 'ok',
error: job.state.lastError, error: job.state.lastError,
duration: job.state.lastDurationMs, duration: job.state.lastDurationMs,
} }
: undefined; : undefined;
// Build nextRun from state // Build nextRun from state
@@ -208,6 +208,12 @@ function registerCronHandlers(gatewayManager: GatewayManager): void {
}) => { }) => {
try { try {
// Transform frontend input to Gateway cron.add format // Transform frontend input to Gateway cron.add format
// For Discord, the recipient must be prefixed with "channel:" or "user:"
const recipientId = input.target.channelId;
const deliveryTo = input.target.channelType === 'discord' && recipientId
? `channel:${recipientId}`
: recipientId;
const gatewayInput = { const gatewayInput = {
name: input.name, name: input.name,
schedule: { kind: 'cron', expr: input.schedule }, schedule: { kind: 'cron', expr: input.schedule },
@@ -218,6 +224,7 @@ function registerCronHandlers(gatewayManager: GatewayManager): void {
delivery: { delivery: {
mode: 'announce', mode: 'announce',
channel: input.target.channelType, channel: input.target.channelType,
to: deliveryTo,
}, },
}; };
const result = await gatewayManager.rpc('cron.add', gatewayInput); const result = await gatewayManager.rpc('cron.add', gatewayInput);

View File

@@ -140,9 +140,11 @@ function TaskDialog({ job, onClose, onSave }: TaskDialogProps) {
const [customSchedule, setCustomSchedule] = useState(''); const [customSchedule, setCustomSchedule] = useState('');
const [useCustom, setUseCustom] = useState(false); const [useCustom, setUseCustom] = useState(false);
const [channelId, setChannelId] = useState(job?.target.channelId || ''); const [channelId, setChannelId] = useState(job?.target.channelId || '');
const [discordChannelId, setDiscordChannelId] = useState('');
const [enabled, setEnabled] = useState(job?.enabled ?? true); const [enabled, setEnabled] = useState(job?.enabled ?? true);
const selectedChannel = channels.find((c) => c.id === channelId); const selectedChannel = channels.find((c) => c.id === channelId);
const isDiscord = selectedChannel?.type === 'discord';
const handleSubmit = async () => { const handleSubmit = async () => {
if (!name.trim()) { if (!name.trim()) {
@@ -157,6 +159,11 @@ function TaskDialog({ job, onClose, onSave }: TaskDialogProps) {
toast.error('Please select a channel'); toast.error('Please select a channel');
return; return;
} }
// Validate Discord channel ID when Discord is selected
if (selectedChannel?.type === 'discord' && !discordChannelId.trim()) {
toast.error('Please enter a Discord Channel ID');
return;
}
const finalSchedule = useCustom ? customSchedule : schedule; const finalSchedule = useCustom ? customSchedule : schedule;
if (!finalSchedule.trim()) { if (!finalSchedule.trim()) {
@@ -166,13 +173,18 @@ function TaskDialog({ job, onClose, onSave }: TaskDialogProps) {
setSaving(true); setSaving(true);
try { try {
// For Discord, use the manually entered channel ID; for others, use empty
const actualChannelId = selectedChannel!.type === 'discord'
? discordChannelId.trim()
: '';
await onSave({ await onSave({
name: name.trim(), name: name.trim(),
message: message.trim(), message: message.trim(),
schedule: finalSchedule, schedule: finalSchedule,
target: { target: {
channelType: selectedChannel!.type, channelType: selectedChannel!.type,
channelId: selectedChannel!.id, channelId: actualChannelId,
channelName: selectedChannel!.name, channelName: selectedChannel!.name,
}, },
enabled, enabled,
@@ -285,6 +297,20 @@ function TaskDialog({ job, onClose, onSave }: TaskDialogProps) {
)} )}
</div> </div>
{/* Discord Channel ID - only shown when Discord is selected */}
{isDiscord && (
<div className="space-y-2">
<Label>Discord Channel ID</Label>
<Input
value={discordChannelId}
onChange={(e) => setDiscordChannelId(e.target.value)}
placeholder="e.g., 1438452657525100686"
/>
<p className="text-xs text-muted-foreground">
Right-click the Discord channel Copy Channel ID
</p>
</div>
)}
{/* Enabled */} {/* Enabled */}
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<div> <div>