| 272 | } |
| 273 | |
| 274 | func (runCtx *runContext) exitCode(ctx context.Context) (int, error) { |
| 275 | if runCtx.attachURL == "" { |
| 276 | return -1, errors.New(ctx, "no attach URL to connect to") |
| 277 | } |
| 278 | |
| 279 | req, err := http.NewRequest("GET", runCtx.attachURL+"/wait", nil) |
| 280 | if err != nil { |
| 281 | return -1, errors.Wrap(ctx, err, "create request") |
| 282 | } |
| 283 | |
| 284 | token, err := runCtx.scalingoClient.GetAccessToken(ctx) |
| 285 | if err != nil { |
| 286 | return -1, errors.Wrap(ctx, err, "get access token") |
| 287 | } |
| 288 | |
| 289 | req.SetBasicAuth("", token) |
| 290 | |
| 291 | res, err := http.DefaultClient.Do(req) |
| 292 | if err != nil { |
| 293 | return -1, errors.Wrap(ctx, err, "send request") |
| 294 | } |
| 295 | defer res.Body.Close() |
| 296 | |
| 297 | body, err := stdio.ReadAll(res.Body) |
| 298 | if err != nil { |
| 299 | return -1, errors.Wrap(ctx, err, "read response body") |
| 300 | } |
| 301 | debug.Println("exit code body:", string(body)) |
| 302 | |
| 303 | if res.StatusCode == http.StatusRequestTimeout { |
| 304 | fmt.Println() |
| 305 | io.Warning("Connection timed out due to inactivity, one-off aborted.") |
| 306 | io.Info("Data should be sent to/from the container regularly to avoid such timeout") |
| 307 | fmt.Println() |
| 308 | io.Info("If you need to run long background tasks, the '--detached' should be used") |
| 309 | io.Info("In this case, output will be available in the main logs of your application:") |
| 310 | io.Info(io.Gray(io.Bold(fmt.Sprintf(" $ scalingo -a %s logs", runCtx.app)))) |
| 311 | return -127, nil |
| 312 | } |
| 313 | |
| 314 | waitRes := map[string]int{} |
| 315 | err = json.NewDecoder(bytes.NewBuffer(body)).Decode(&waitRes) |
| 316 | if err != nil { |
| 317 | return -1, errors.Wrap(ctx, err, "decode response") |
| 318 | } |
| 319 | |
| 320 | return waitRes["exit_code"], nil |
| 321 | } |
| 322 | |
| 323 | func (runCtx *runContext) connectToRunServer(ctx context.Context) (*http.Response, net.Conn, error) { |
| 324 | if runCtx.attachURL == "" { |