CmdAdd handles the "add" command.
(fn Function)
| 425 | |
| 426 | // CmdAdd handles the "add" command. |
| 427 | func (h *Handler[C]) CmdAdd(fn Function) { |
| 428 | if err := fn.ValidateArgs(3); err != nil { |
| 429 | h.api.SendCodef(fn, 400, "%v", err) |
| 430 | return |
| 431 | } |
| 432 | |
| 433 | key, name, ok := h.cb.ExtractKey(fn) |
| 434 | if !ok { |
| 435 | h.api.SendCodef(fn, 400, "invalid job ID format.") |
| 436 | return |
| 437 | } |
| 438 | |
| 439 | if err := fn.ValidateHasPayload(); err != nil { |
| 440 | h.api.SendCodef(fn, 400, "%v", err) |
| 441 | return |
| 442 | } |
| 443 | |
| 444 | if err := h.cb.ValidateJobName(name); err != nil { |
| 445 | h.api.SendCodef(fn, 400, "invalid job name '%s': %v.", name, err) |
| 446 | return |
| 447 | } |
| 448 | |
| 449 | newCfg, err := h.cb.ParseAndValidate(fn, name) |
| 450 | if err != nil { |
| 451 | h.api.SendCodef(fn, 400, "%v", err) |
| 452 | return |
| 453 | } |
| 454 | |
| 455 | // Replace existing config at the same key, if any. |
| 456 | if existing, ok := h.exposed.LookupByKey(key); ok { |
| 457 | if _, found := h.seen.Lookup(existing.Cfg); found && existing.Cfg.SourceType() == "dyncfg" { |
| 458 | h.seen.Remove(existing.Cfg) |
| 459 | } |
| 460 | h.exposed.Remove(existing.Cfg) |
| 461 | h.cb.Stop(existing.Cfg) |
| 462 | } |
| 463 | |
| 464 | h.seen.Add(newCfg) |
| 465 | newEntry := &Entry[C]{Cfg: newCfg, Status: StatusAccepted} |
| 466 | h.exposed.Add(newEntry) |
| 467 | |
| 468 | h.api.SendCodef(fn, 202, "") |
| 469 | h.NotifyJobCreate(newCfg, StatusAccepted) |
| 470 | } |
| 471 | |
| 472 | // CmdEnable handles the "enable" command. |
| 473 | func (h *Handler[C]) CmdEnable(fn Function) { |