github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/appengine/instance_mgt/main.go (about)

     1  /*  Package instance_mgt tries to send and receive data to sibling instances.
     2  
     3  
     4  Remember, there is a *quota* on getIntance...()  requests.
     5  
     6  Make instance info available in Memory.
     7  
     8  
     9  Have it refresh upon init and
    10   every xxx minutes.
    11  
    12  There is a strange "version suffix".
    13  We call it VersionMinor and chop it off.
    14  
    15  
    16  
    17  */
    18  package instance_mgt
    19  
    20  import (
    21  	"net/http"
    22  
    23  	"google.golang.org/appengine"
    24  
    25  	"bytes"
    26  	"fmt"
    27  	"log"
    28  	"time"
    29  
    30  	aelog "google.golang.org/appengine/log"
    31  )
    32  
    33  type Instance struct {
    34  	InstanceID    string
    35  	VersionFull   string // v2.243253253
    36  	VersionMajor  string // v2
    37  	VersionMinor  string //    243253253
    38  	ModuleName    string
    39  	NumInstances  int
    40  	Hostname      string
    41  	HostnameInst0 string
    42  	HostnameInst1 string
    43  	HostnameMod02 string
    44  	LastUpdated   time.Time
    45  
    46  	PureHostname string // without version, module
    47  }
    48  
    49  //var info = new(Instance)
    50  // var ci chan *Instance = make(chan *Instance)
    51  
    52  //  &proto.RequiredNotSetError{field:"{Unknown}"}
    53  const autoScalingErr1 = `proto: required field "{Unknown}" not set`
    54  const autoScalingErr2 = `API error 3 (modules: INVALID_INSTANCES): The specified instance does not exist for this module/version.`
    55  const autoScalingErrMsg = `NotSupportedWithautoScalingError`
    56  
    57  func (i *Instance) String() string {
    58  
    59  	if i == nil {
    60  		return "<empty pointer to instance info>"
    61  	}
    62  
    63  	b1 := new(bytes.Buffer)
    64  	b1.WriteString(fmt.Sprintf("Module %q Version %q %q \n", i.ModuleName, i.VersionMajor, i.VersionMinor))
    65  	b1.WriteString(fmt.Sprintf("Number of Instances %v \n", i.NumInstances))
    66  	b1.WriteString(fmt.Sprintf("Instance Id %q  \n", i.InstanceID))
    67  	b1.WriteString("\n")
    68  
    69  	b1.WriteString(fmt.Sprintf("Hostname Pure                       %q \n", i.PureHostname))
    70  	b1.WriteString(fmt.Sprintf("Hostname Module 'default' Inst 'x'  %q \n", i.Hostname))
    71  	b1.WriteString(fmt.Sprintf("Hostname Module '%v' Inst '0'  %q \n", i.ModuleName, i.HostnameInst0))
    72  	b1.WriteString(fmt.Sprintf("Hostname Module '%v' Inst '1'  %q \n", i.ModuleName, i.HostnameInst1))
    73  	b1.WriteString(fmt.Sprintf("Hostname Module 'mod02'   Inst 'x'  %q \n", i.HostnameMod02))
    74  
    75  	b1.WriteString("\n")
    76  
    77  	return b1.String()
    78  }
    79  
    80  func onStart(w http.ResponseWriter, r *http.Request) {
    81  	c := appengine.NewContext(r)
    82  	aelog.Infof(c, "instance started by appengine")
    83  
    84  	// func() {
    85  	// 	time.Sleep(200 * time.Millisecond)
    86  	// 	collectInfo(w, r, map[string]interface{}{})
    87  	// }()
    88  
    89  }
    90  
    91  func onStop(w http.ResponseWriter, r *http.Request) {
    92  	c := appengine.NewContext(r)
    93  	aelog.Infof(c, "instance stopped by appengine")
    94  }
    95  
    96  func init() {
    97  
    98  	// InstanceId := appengine.InstanceID() // does not during init, only after a few seconds
    99  	http.HandleFunc("/_ah/start", onStart)
   100  	http.HandleFunc("/_ah/stop", onStop)
   101  
   102  	go func() {
   103  		for {
   104  			if ii.LastUpdated.IsZero() {
   105  				time.Sleep(36 * time.Second)
   106  				continue
   107  			}
   108  
   109  			s := fmt.Sprintf("hostname is %v, pure hostname is %v (%v)", ii.Hostname, ii.PureHostname, time.Now().Sub(ii.LastUpdated))
   110  			log.Println(s)
   111  			time.Sleep(36 * time.Second)
   112  		}
   113  
   114  	}()
   115  
   116  }