( c *gin.Context, pinned pluginruntime.PinnedEndpoint, protocolRequest pluginruntime.ProtocolRequestContext, taskID string, machine *relay.PluginResponsesMachine, deps pluginProtocolBridgeDeps, )
| 398 | } |
| 399 | |
| 400 | func streamTaskPluginProtocol( |
| 401 | c *gin.Context, |
| 402 | pinned pluginruntime.PinnedEndpoint, |
| 403 | protocolRequest pluginruntime.ProtocolRequestContext, |
| 404 | taskID string, |
| 405 | machine *relay.PluginResponsesMachine, |
| 406 | deps pluginProtocolBridgeDeps, |
| 407 | ) { |
| 408 | generation := pinned.Generation.Number |
| 409 | pluginKey := pinned.Plugin.Meta.Key |
| 410 | logger.LogDebug( |
| 411 | c, |
| 412 | "task_plugin subsystem=protocol event=observation_start generation=%d plugin=%q mode=stream public_task_id=%q timeout_ms=%d tick_ms=%d heartbeat_ms=%d", |
| 413 | generation, |
| 414 | pluginKey, |
| 415 | taskID, |
| 416 | deps.observationTimeout.Milliseconds(), |
| 417 | deps.tickInterval.Milliseconds(), |
| 418 | deps.heartbeatInterval.Milliseconds(), |
| 419 | ) |
| 420 | created, err := machine.CreatedEvent() |
| 421 | if err != nil { |
| 422 | logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=stream stage=created_event reason=state_machine_error", generation, pluginKey) |
| 423 | respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed") |
| 424 | return |
| 425 | } |
| 426 | helper.SetEventStreamHeaders(c) |
| 427 | if err = writeTaskPluginProtocolEvent(c, created); err != nil { |
| 428 | logger.LogDebug(c, "task_plugin subsystem=protocol event=client_write_failed generation=%d plugin=%q mode=stream stage=created_event", generation, pluginKey) |
| 429 | return |
| 430 | } |
| 431 | |
| 432 | observationContext, cancelObservation := context.WithTimeout(c.Request.Context(), deps.observationTimeout) |
| 433 | defer cancelObservation() |
| 434 | heartbeatTicker := time.NewTicker(deps.heartbeatInterval) |
| 435 | defer heartbeatTicker.Stop() |
| 436 | |
| 437 | var previous relay.ProtocolState |
| 438 | tickNumber := uint64(0) |
| 439 | lastStatus := "" |
| 440 | for { |
| 441 | loadStarted := deps.now() |
| 442 | loadContext, cancelLoad := context.WithTimeout(observationContext, deps.loadTimeout) |
| 443 | task, exists, loadErr := deps.loadTask( |
| 444 | loadContext, |
| 445 | common.GetContextKeyInt(c, constant.ContextKeyUserId), |
| 446 | constant.TaskPlatform(pinned.Plugin.Meta.Key), |
| 447 | taskID, |
| 448 | ) |
| 449 | loadContextErr := loadContext.Err() |
| 450 | cancelLoad() |
| 451 | loadElapsed := deps.now().Sub(loadStarted) |
| 452 | if errors.Is(loadContextErr, context.DeadlineExceeded) && |
| 453 | observationContext.Err() == nil && |
| 454 | c.Request.Context().Err() == nil { |
| 455 | logger.LogWarn(c, common.LogText( |
| 456 | "task protocol database observation overloaded; plugin=%s task=%s", |
| 457 | pinned.Plugin.Meta.Key, |
no test coverage detected