| 314 | } |
| 315 | |
| 316 | func removeWorker(c *cli.Context) error { |
| 317 | args := c.Args().Slice() |
| 318 | if len(args) != 0 { |
| 319 | return cli.Exit("Usage: tunasynctl -w <worker-id>", 1) |
| 320 | } |
| 321 | workerID := c.String("worker") |
| 322 | if len(workerID) == 0 { |
| 323 | return cli.Exit("Please specify the <worker-id>", 1) |
| 324 | } |
| 325 | url := fmt.Sprintf("%s/workers/%s", baseURL, workerID) |
| 326 | |
| 327 | req, err := http.NewRequest("DELETE", url, nil) |
| 328 | if err != nil { |
| 329 | logger.Panicf("Invalid HTTP Request: %s", err.Error()) |
| 330 | } |
| 331 | resp, err := client.Do(req) |
| 332 | |
| 333 | if err != nil { |
| 334 | return cli.Exit( |
| 335 | fmt.Sprintf("Failed to send request to manager: %s", err.Error()), 1) |
| 336 | } |
| 337 | defer resp.Body.Close() |
| 338 | |
| 339 | if resp.StatusCode != http.StatusOK { |
| 340 | body, err := io.ReadAll(resp.Body) |
| 341 | if err != nil { |
| 342 | return cli.Exit( |
| 343 | fmt.Sprintf("Failed to parse response: %s", err.Error()), |
| 344 | 1) |
| 345 | } |
| 346 | |
| 347 | return cli.Exit(fmt.Sprintf("Failed to correctly send"+ |
| 348 | " command: HTTP status code is not 200: %s", body), |
| 349 | 1) |
| 350 | } |
| 351 | |
| 352 | res := map[string]string{} |
| 353 | _ = json.NewDecoder(resp.Body).Decode(&res) |
| 354 | if res["message"] == "deleted" { |
| 355 | fmt.Println("Successfully removed the worker") |
| 356 | } else { |
| 357 | return cli.Exit("Failed to remove the worker", 1) |
| 358 | } |
| 359 | return nil |
| 360 | } |
| 361 | |
| 362 | func flushDisabledJobs(c *cli.Context) error { |
| 363 | req, err := http.NewRequest("DELETE", baseURL+flushDisabledPath, nil) |