Skip to content

Commit

Permalink
Merge pull request #1317 from ndisidore/nathan/feat/workers-tail-cons…
Browse files Browse the repository at this point in the history
…umers
  • Loading branch information
jacobbednarz committed Jun 23, 2023
2 parents e159d2c + 899550c commit 84af07d
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .changelog/1317.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers: Add ability to specify tail Workers in script metadata
```
53 changes: 34 additions & 19 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ type CreateWorkerParams struct {
// ES Module syntax script.
Module bool

// Logpush opts the worker into Workers Logpush logging. A nil value leaves the current setting unchanged.
// https://developers.cloudflare.com/workers/platform/logpush/
// Logpush opts the worker into Workers Logpush logging. A nil value leaves
// the current setting unchanged.
//
// Documentation: https://developers.cloudflare.com/workers/platform/logpush/
Logpush *bool

// TailConsumers specifies a list of Workers that will consume the logs of
// the attached Worker.
// Documentation: https://developers.cloudflare.com/workers/platform/tail-workers/
TailConsumers *[]WorkersTailConsumer

// Bindings should be a map where the keys are the binding name, and the
// values are the binding content
Bindings map[string]WorkerBinding
Expand Down Expand Up @@ -90,17 +97,23 @@ type WorkerScript struct {
UsageModel string `json:"usage_model,omitempty"`
}

type WorkersTailConsumer struct {
Service string `json:"service"`
Environment *string `json:"environment,omitempty"`
}

// WorkerMetaData contains worker script information such as size, creation & modification dates.
type WorkerMetaData struct {
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Logpush *bool `json:"logpush,omitempty"`
LastDeployedFrom *string `json:"last_deployed_from,omitempty"`
DeploymentId *string `json:"deployment_id,omitempty"`
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Logpush *bool `json:"logpush,omitempty"`
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
LastDeployedFrom *string `json:"last_deployed_from,omitempty"`
DeploymentId *string `json:"deployment_id,omitempty"`
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`
}

// WorkerListResponse wrapper struct for API response to worker script list API call.
Expand Down Expand Up @@ -251,7 +264,7 @@ func (api *API) UploadWorker(ctx context.Context, rc *ResourceContainer, params
err error
)

if params.Module || params.Logpush != nil || params.Placement != nil || len(params.Bindings) > 0 || params.CompatibilityDate != "" || len(params.CompatibilityFlags) > 0 {
if params.Module || params.Logpush != nil || params.Placement != nil || len(params.Bindings) > 0 || params.CompatibilityDate != "" || len(params.CompatibilityFlags) > 0 || params.TailConsumers != nil {
contentType, body, err = formatMultipartBody(params)
if err != nil {
return WorkerScriptResponse{}, err
Expand Down Expand Up @@ -285,16 +298,18 @@ func formatMultipartBody(params CreateWorkerParams) (string, []byte, error) {
// Write metadata part
var scriptPartName string
meta := struct {
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
}{
Bindings: make([]workerBindingMeta, 0, len(params.Bindings)),
Logpush: params.Logpush,
TailConsumers: params.TailConsumers,
CompatibilityDate: params.CompatibilityDate,
CompatibilityFlags: params.CompatibilityFlags,
Placement: params.Placement,
Expand Down

0 comments on commit 84af07d

Please sign in to comment.