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 dm "github.com/luci/luci-go/common/api/dm/service/v1" |
| 13 "github.com/luci/luci-go/common/gcloud/pubsub" |
| 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 state PersistentState) *TaskDescription { |
| 23 return &TaskDescription{ |
| 24 c: c, |
| 25 payload: payload, |
| 26 executionAuth: exAuth, |
| 27 previousState: state, |
| 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 previousState PersistentState |
| 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 // PreviousState is the current PersistentState of the Attempt (e.g. the |
| 56 // PersistentState returned by the previous Execution). This will be empty |
| 57 // for the first Execution. |
| 58 func (t *TaskDescription) PreviousState() PersistentState { |
| 59 return t.previousState |
| 60 } |
| 61 |
| 62 // Payload is description of the job to run. |
| 63 func (t *TaskDescription) Payload() *dm.Quest_Desc { |
| 64 return proto.Clone(t.payload).(*dm.Quest_Desc) |
| 65 } |
| 66 |
| 67 // ExecutionAuth is the combined execution_id+activation token that the |
| 68 // execution must use to call ActivateExecution before making further API calls |
| 69 // into DM. |
| 70 func (t *TaskDescription) ExecutionAuth() *dm.Execution_Auth { |
| 71 ret := *t.executionAuth |
| 72 return &ret |
| 73 } |
OLD | NEW |