| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package distributor | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 | |
| 10 "github.com/golang/protobuf/proto" | |
| 11 "github.com/luci/gae/service/info" | |
| 12 "github.com/luci/luci-go/common/gcloud/pubsub" | |
| 13 dm "github.com/luci/luci-go/dm/api/service/v1" | |
| 14 "golang.org/x/net/context" | |
| 15 ) | |
| 16 | |
| 17 // NewTaskDescription builds a new *TaskDescription. | |
| 18 // | |
| 19 // It's intended for use by the DM core logic, and not for use by distributor | |
| 20 // implementations. | |
| 21 func NewTaskDescription(c context.Context, payload *dm.Quest_Desc, exAuth *dm.Ex
ecution_Auth, | |
| 22 previousResult *dm.JsonResult) *TaskDescription { | |
| 23 return &TaskDescription{ | |
| 24 c: c, | |
| 25 payload: payload, | |
| 26 executionAuth: exAuth, | |
| 27 previousResult: previousResult, | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 // TaskDescription is the parameters for PrepareTask. | |
| 32 type TaskDescription struct { | |
| 33 c context.Context | |
| 34 payload *dm.Quest_Desc | |
| 35 executionAuth *dm.Execution_Auth | |
| 36 previousResult *dm.JsonResult | |
| 37 } | |
| 38 | |
| 39 // PrepareTopic returns the pubsub topic that notifications should be sent to. | |
| 40 // | |
| 41 // It returns the full name of the topic and a token that will be used to route | |
| 42 // PubSub messages back to the Distributor. The publisher to the topic must be | |
| 43 // instructed to put the token into the 'auth_token' attribute of PubSub | |
| 44 // messages. DM will know how to route such messages to D.HandleNotification. | |
| 45 func (t *TaskDescription) PrepareTopic() (topic pubsub.Topic, token string, err
error) { | |
| 46 topic = pubsub.NewTopic(info.Get(t.c).TrimmedAppID(), notifyTopicSuffix) | |
| 47 if err := topic.Validate(); err != nil { | |
| 48 panic(fmt.Errorf("failed to validate Topic %q: %s", topic, err)) | |
| 49 } | |
| 50 token, err = encodeAuthToken(t.c, t.executionAuth.Id, | |
| 51 t.payload.DistributorConfigName) | |
| 52 return | |
| 53 } | |
| 54 | |
| 55 // PreviousResult is the Result of the last successful Execution for the | |
| 56 // Attempt. This will be nil for the first Execution. | |
| 57 func (t *TaskDescription) PreviousResult() *dm.JsonResult { | |
| 58 if t.previousResult == nil { | |
| 59 return nil | |
| 60 } | |
| 61 return proto.Clone(t.previousResult).(*dm.JsonResult) | |
| 62 } | |
| 63 | |
| 64 // Payload is description of the job to run. | |
| 65 func (t *TaskDescription) Payload() *dm.Quest_Desc { | |
| 66 return proto.Clone(t.payload).(*dm.Quest_Desc) | |
| 67 } | |
| 68 | |
| 69 // ExecutionAuth is the combined execution_id+activation token that the | |
| 70 // execution must use to call ActivateExecution before making further API calls | |
| 71 // into DM. | |
| 72 func (t *TaskDescription) ExecutionAuth() *dm.Execution_Auth { | |
| 73 ret := *t.executionAuth | |
| 74 return &ret | |
| 75 } | |
| OLD | NEW |