vitess.io/vitess@v0.16.2/go/vt/events/status.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package events defines common structures used for events dispatched from 18 // various other package. 19 package events 20 21 import ( 22 "time" 23 ) 24 25 // StatusUpdater is a base struct for multi-part events with a status string 26 // that gets updated as the process progresses. StatusUpdater implements 27 // event.Updater, so if you embed a StatusUpdater into an event type, you can 28 // set a new status and dispatch that event in one call with DispatchUpdate. 29 // 30 // For example: 31 // 32 // type MyEvent struct { 33 // StatusUpdater 34 // } 35 // ev := &MyEvent{} 36 // event.DispatchUpdate(ev, "new status") 37 type StatusUpdater struct { 38 Status string 39 40 // EventID is used to group the steps of a multi-part event. 41 // It is set internally the first time Update() is called. 42 EventID int64 43 } 44 45 // Update sets a new status and initializes the EventID if necessary. 46 // This implements event.Updater.Update(). 47 func (su *StatusUpdater) Update(status any) { 48 su.Status = status.(string) 49 50 // initialize event ID 51 if su.EventID == 0 { 52 su.EventID = time.Now().UnixNano() 53 } 54 }