Edits history of script submission #23603 for ' Update Schedule (windmill)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    import * as wmill from "windmill-client"
    
    // windmill-client errors only carry the status text: throw the response body, which says why
    wmill.OpenAPI.interceptors.response.use(async (response) => {
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      return response
    })
    
    export type DynSelect_path = string
    
    export async function path() {
      const schedules = await wmill.ScheduleService.listSchedules({
        workspace: wmill.getWorkspace(),
        perPage: 1000,
      })
      return schedules.map((s) => ({
        value: s.path,
        label: `${s.path} (${s.schedule})`,
      }))
    }
    
    /**
     * Update Schedule
     * Change a schedule's cron expression, timezone, args or summary. Fields left empty keep their current value; use Enable or Disable Schedule to toggle it.
     */
    export async function main(
      path: DynSelect_path,
      cron: string | undefined,
      timezone: string | undefined,
      args: { [key: string]: any } | undefined,
      summary: string | undefined
    ) {
      const ws = wmill.getWorkspace()
      // the update endpoint replaces the schedule, so start from the current one
      const current = await wmill.ScheduleService.getSchedule({
        workspace: ws,
        path,
      })
      return await wmill.ScheduleService.updateSchedule({
        workspace: ws,
        path,
        requestBody: {
          ...current,
          schedule: cron || current.schedule,
          timezone: timezone || current.timezone,
          args: args ?? current.args ?? {},
          summary: summary || current.summary,
        },
      })
    }
    

    Submitted by hugo989 5 hours ago