Skip to content
This repository
tree: e135a5ba8a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 203 lines (167 sloc) 4.348 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
// Copyright (c) 2012, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/soundcloud/visor

package visor

import (
"fmt"
"net"
"path"
"strconv"
"strings"
"time"
)

const (
InsStateInitial State = "initial"
InsStateStarted = "started"
InsStateFailed = "failed"
InsStateDead = "dead"
InsStateExited = "exited"
)

const instancesPath = "instances"

// An Instance represents a running process of a specific type.
type Instance struct {
dir
Name string
AppName string
RevisionName string
ProcessName string
ServiceName string
Host string
Port int
State State
}

// NewInstance creates and returns a new Instance object.
func NewInstance(pty string, rev string, app string, addr string, snapshot Snapshot) (ins *Instance, err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return
}

ins = &Instance{
Host: tcpAddr.IP.String(),
Port: tcpAddr.Port,
ServiceName: app + "-" + pty,
AppName: app,
ProcessName: pty,
RevisionName: rev,
State: InsStateInitial,
}
ins.dir = dir{snapshot, "/instances/" + ins.Id()}
ins.Name = ins.Id()

return
}

// FastForward advances the instance in time. It returns
// a new instance of Instance with the supplied revision.
func (i *Instance) FastForward(rev int64) *Instance {
return i.Snapshot.fastForward(i, rev).(*Instance)
}

func (i *Instance) createSnapshot(rev int64) snapshotable {
tmp := *i
tmp.Snapshot = Snapshot{rev, i.conn}
return &tmp
}

func (i *Instance) proctypePath() string {
return path.Join(appsPath, i.AppName, procsPath, string(i.ProcessName), instancesPath, i.Id())
}

// Register registers an instance with the registry.
func (i *Instance) Register() (instance *Instance, err error) {
exists, _, err := i.conn.Exists(i.dir.Name)
if err != nil {
return
}
if exists {
return nil, ErrKeyConflict
}

_, err = i.set("info", i.String())
if err != nil {
return i, err
}
_, err = i.set("state", string(i.State))
if err != nil {
return i, err
}
now := time.Now().UTC().String()

s, err := i.Snapshot.set(i.proctypePath(), now)
instance = i.FastForward(s.Rev)

return
}

// Unregister unregisters an instance with the registry.
func (i *Instance) Unregister() (err error) {
err = i.Snapshot.del(i.proctypePath())
if err != nil {
return
}
err = i.del("/")
return
}

// UpdateState updates the instance's state file in
// the coordinator to the given value.
func (i *Instance) UpdateState(s State) (ins *Instance, err error) {
newrev, err := i.set("state", string(s))
if err != nil {
return
}
ins = i.FastForward(newrev)
ins.State = s

return
}

func (i *Instance) Id() string {
return fmt.Sprintf("%s-%d", strings.Replace(i.Host, ".", "-", -1), i.Port)
}

func (i *Instance) String() string {
return strings.Join([]string{
i.AppName,
i.RevisionName,
string(i.ProcessName),
i.Host,
fmt.Sprintf("%d", i.Port),
}, " ")
}

func (i *Instance) AddrString() string {
return i.Host + ":" + strconv.Itoa(i.Port)
}

func (i *Instance) RefString() string {
return fmt.Sprintf("%s:%s@%s", i.AppName, i.ProcessName, i.RevisionName)
}

func (i *Instance) LogString() string {
return fmt.Sprintf("%s (%s)", i.RefString(), i.AddrString())
}

// GetInstance returns an Instance from the given app, rev, proc and instance ids.
func GetInstance(s Snapshot, insName string) (ins *Instance, err error) {
p := path.Join(instancesPath, insName)

state, _, err := s.get(p + "/state")
if err != nil {
return
}

info, _, err := s.get(p + "/info")
if err != nil {
return
}
fields := strings.Fields(info)

addr := fields[3] + ":" + fields[4]

ins, err = NewInstance(fields[2], fields[1], fields[0], addr, s)
if err != nil {
return
}
ins.State = State(state)

return
}

func Instances(s Snapshot) (ins []*Instance, err error) {
exists, _, err := s.conn.Exists(instancesPath)
if err != nil || !exists {
return
}

names, err := s.FastForward(-1).getdir(instancesPath)
if err != nil {
return
}

for i := range names {
var instance *Instance

instance, err = GetInstance(s, names[i])
if err != nil {
return
}

ins = append(ins, instance)
}

return
}
Something went wrong with that request. Please try again.