本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/technoweenie/grohl.NewContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewContext函數的具體用法?Golang NewContext怎麽用?Golang NewContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: watchFormations
func (c *context) watchFormations(events chan<- *FormationEvent, hostEvents chan<- *host.Event) {
g := grohl.NewContext(grohl.Data{"fn": "watchFormations"})
c.syncCluster(hostEvents)
if events != nil {
events <- &FormationEvent{}
}
var attempts int
var lastUpdatedAt time.Time
for {
// wait a second if we've tried more than once
attempts++
if attempts > 1 {
time.Sleep(time.Second)
}
g.Log(grohl.Data{"at": "connect", "attempt": attempts})
updates, err := c.StreamFormations(&lastUpdatedAt)
for ef := range updates.Chan {
// we are now connected so reset attempts
attempts = 0
if ef.App == nil {
// sentinel
continue
}
lastUpdatedAt = ef.UpdatedAt
f := c.formations.Get(ef.App.ID, ef.Release.ID)
if f != nil {
g.Log(grohl.Data{"app.id": ef.App.ID, "release.id": ef.Release.ID, "at": "update"})
f.SetProcesses(ef.Processes)
} else {
g.Log(grohl.Data{"app.id": ef.App.ID, "release.id": ef.Release.ID, "at": "new"})
f = NewFormation(c, ef)
c.formations.Add(f)
}
// check for omnipresence
for _, proctype := range f.Release.Processes {
if proctype.Omni {
c.omniMtx.Lock()
c.omni[f] = struct{}{}
c.omniMtx.Unlock()
break
}
}
go func() {
f.Rectify()
if events != nil {
events <- &FormationEvent{Formation: f}
}
}()
}
if *err != nil {
g.Log(grohl.Data{"at": "error", "error": *err})
}
g.Log(grohl.Data{"at": "disconnect"})
updates.Close()
}
}
示例2: remove
func (f *Formation) remove(n int, name string, hostID string) {
g := grohl.NewContext(grohl.Data{"fn": "remove", "app.id": f.AppID, "release.id": f.Release.ID})
i := 0
sj := make(sortJobs, 0, len(f.jobs[name]))
for _, job := range f.jobs[name] {
sj = append(sj, job)
}
sj.Sort()
for _, job := range sj {
g.Log(grohl.Data{"host.id": job.HostID, "job.id": job.ID})
if hostID != "" && job.HostID != hostID { // remove from a specific host
continue
}
// TODO: robust host handling
if err := f.c.hosts.Get(job.HostID).StopJob(job.ID); err != nil {
g.Log(grohl.Data{"at": "error", "err": err.Error()})
// TODO: handle error
}
f.jobs.Remove(job)
if i++; i == n {
break
}
}
}
示例3: cleanup
func (c *libvirtContainer) cleanup() error {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "cleanup", "job.id": c.job.ID})
g.Log(grohl.Data{"at": "start"})
if err := syscall.Unmount(filepath.Join(c.RootPath, ".containerinit"), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "file": ".containerinit", "status": "error", "err": err})
}
if err := syscall.Unmount(filepath.Join(c.RootPath, "etc/resolv.conf"), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "file": "resolv.conf", "status": "error", "err": err})
}
if err := pinkerton.Cleanup(c.job.ID); err != nil {
g.Log(grohl.Data{"at": "pinkerton", "status": "error", "err": err})
}
for _, m := range c.job.Config.Mounts {
if err := syscall.Unmount(filepath.Join(c.RootPath, m.Location), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "location": m.Location, "status": "error", "err": err})
}
}
for _, p := range c.job.Config.Ports {
if err := c.l.forwarder.Remove(&net.TCPAddr{IP: c.IP, Port: p.Port}, p.RangeEnd, p.Proto); err != nil {
g.Log(grohl.Data{"at": "iptables", "status": "error", "err": err, "port": p.Port})
}
c.l.ports[p.Proto].Put(uint16(p.Port))
}
ipallocator.ReleaseIP(defaultNet, &c.IP)
g.Log(grohl.Data{"at": "finish"})
return nil
}
示例4: drainTo
func (m *LogMux) drainTo(w io.Writer) {
defer close(m.donec)
g := grohl.NewContext(grohl.Data{"at": "logmux_drain"})
for {
msg, ok := <-m.logc
if !ok {
return // shutdown
}
_, err := w.Write(rfc6587.Bytes(msg))
if err != nil {
g.Log(grohl.Data{"status": "error", "err": err.Error()})
// write logs to local logger when the writer fails
g.Log(grohl.Data{"msg": msg.String()})
for msg := range m.logc {
g.Log(grohl.Data{"msg": msg.String()})
}
return // shutdown
}
}
}
示例5: cleanup
func (c *libvirtContainer) cleanup() error {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "cleanup", "job.id": c.job.ID})
g.Log(grohl.Data{"at": "start"})
if err := syscall.Unmount(filepath.Join(c.RootPath, ".containerinit"), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "file": ".containerinit", "status": "error", "err": err})
}
if err := syscall.Unmount(filepath.Join(c.RootPath, "etc/resolv.conf"), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "file": "resolv.conf", "status": "error", "err": err})
}
if err := c.l.pinkerton.Cleanup(c.job.ID); err != nil {
g.Log(grohl.Data{"at": "pinkerton", "status": "error", "err": err})
}
for _, m := range c.job.Config.Mounts {
if err := syscall.Unmount(filepath.Join(c.RootPath, m.Location), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "location": m.Location, "status": "error", "err": err})
}
}
for _, v := range c.job.Config.Volumes {
if err := syscall.Unmount(filepath.Join(c.RootPath, v.Target), 0); err != nil {
g.Log(grohl.Data{"at": "unmount", "target": v.Target, "volumeID": v.VolumeID, "status": "error", "err": err})
}
}
if !c.job.Config.HostNetwork && c.l.bridgeNet != nil {
c.l.ipalloc.ReleaseIP(c.l.bridgeNet, c.IP)
}
g.Log(grohl.Data{"at": "finish"})
return nil
}
示例6: Cleanup
func (l *LibvirtLXCBackend) Cleanup() error {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "Cleanup"})
l.containersMtx.Lock()
ids := make([]string, 0, len(l.containers))
for id := range l.containers {
ids = append(ids, id)
}
l.containersMtx.Unlock()
g.Log(grohl.Data{"at": "start", "count": len(ids)})
errs := make(chan error)
for _, id := range ids {
go func(id string) {
g.Log(grohl.Data{"at": "stop", "job.id": id})
err := l.Stop(id)
if err != nil {
g.Log(grohl.Data{"at": "error", "job.id": id, "err": err.Error()})
}
errs <- err
}(id)
}
var err error
for i := 0; i < len(ids); i++ {
stopErr := <-errs
if stopErr != nil {
err = stopErr
}
}
g.Log(grohl.Data{"at": "finish"})
return err
}
示例7: waitExit
// waitExit waits for the libvirt domain to be marked as done or five seconds to
// elapse
func (c *libvirtContainer) waitExit() {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "waitExit", "job.id": c.job.ID})
g.Log(grohl.Data{"at": "start"})
domain, err := c.l.libvirt.LookupDomainByName(c.job.ID)
if err != nil {
g.Log(grohl.Data{"at": "domain_error", "err": err.Error()})
return
}
defer domain.Free()
maxWait := time.After(5 * time.Second)
for {
state, err := domain.GetState()
if err != nil {
g.Log(grohl.Data{"at": "state_error", "err": err.Error()})
return
}
if state[0] != libvirt.VIR_DOMAIN_RUNNING && state[0] != libvirt.VIR_DOMAIN_SHUTDOWN {
g.Log(grohl.Data{"at": "done"})
return
}
select {
case <-maxWait:
g.Log(grohl.Data{"at": "maxWait"})
return
default:
time.Sleep(100 * time.Millisecond)
}
}
}
示例8: attach
func (h *attachHandler) attach(req *host.AttachReq, conn io.ReadWriteCloser) {
defer conn.Close()
g := grohl.NewContext(grohl.Data{"fn": "attach", "job.id": req.JobID})
g.Log(grohl.Data{"at": "start"})
attachWait := make(chan struct{})
job := h.state.AddAttacher(req.JobID, attachWait)
if job == nil {
defer h.state.RemoveAttacher(req.JobID, attachWait)
if _, err := conn.Write([]byte{host.AttachWaiting}); err != nil {
return
}
// TODO: add timeout
<-attachWait
job = h.state.GetJob(req.JobID)
}
success := make(chan struct{})
failed := make(chan struct{})
opts := &AttachRequest{
Job: job,
Logs: req.Flags&host.AttachFlagLogs != 0,
Stream: req.Flags&host.AttachFlagStream != 0,
Height: req.Height,
Width: req.Width,
Attached: success,
ReadWriter: conn,
Streams: make([]string, 0, 3),
}
if req.Flags&host.AttachFlagStdin != 0 {
opts.Streams = append(opts.Streams, "stdin")
}
if req.Flags&host.AttachFlagStdout != 0 {
opts.Streams = append(opts.Streams, "stdout")
}
if req.Flags&host.AttachFlagStderr != 0 {
opts.Streams = append(opts.Streams, "stderr")
}
go func() {
select {
case <-success:
conn.Write([]byte{host.AttachSuccess})
close(success)
case <-failed:
}
close(attachWait)
}()
if err := h.backend.Attach(opts); err != nil {
select {
case <-success:
default:
close(failed)
conn.Write(append([]byte{host.AttachError}, err.Error()...))
}
g.Log(grohl.Data{"status": "error", "err": err})
return
}
g.Log(grohl.Data{"at": "finish"})
}
示例9: watchFormations
func (c *context) watchFormations() {
g := grohl.NewContext(grohl.Data{"fn": "watchFormations"})
c.syncCluster()
var attempts int
var lastUpdatedAt time.Time
for {
// wait a second if we've tried more than once
attempts++
if attempts > 1 {
time.Sleep(time.Second)
}
g.Log(grohl.Data{"at": "connect", "attempt": attempts})
updates := make(chan *ct.ExpandedFormation)
streamCtrl, err := c.StreamFormations(&lastUpdatedAt, updates)
if err != nil {
g.Log(grohl.Data{"at": "error", "error": err})
continue
}
for ef := range updates {
// we are now connected so reset attempts
attempts = 0
if ef.App == nil {
// sentinel
continue
}
lastUpdatedAt = ef.UpdatedAt
f := c.formations.Get(ef.App.ID, ef.Release.ID)
if f != nil {
g.Log(grohl.Data{"app.id": ef.App.ID, "release.id": ef.Release.ID, "at": "update"})
f.SetProcesses(ef.Processes)
} else {
g.Log(grohl.Data{"app.id": ef.App.ID, "release.id": ef.Release.ID, "at": "new"})
f = NewFormation(c, ef)
c.formations.Add(f)
}
// check for omnipresence
for _, proctype := range f.Release.Processes {
if proctype.Omni {
c.omniMtx.Lock()
c.omni[f] = struct{}{}
c.omniMtx.Unlock()
break
}
}
go f.Rectify()
}
if streamCtrl.Err() != nil {
g.Log(grohl.Data{"at": "disconnect", "err": streamCtrl.Err()})
}
g.Log(grohl.Data{"at": "disconnect"})
}
}
示例10: add
func (f *Formation) add(n int, name string, hostID string) {
g := grohl.NewContext(grohl.Data{"fn": "add", "app.id": f.AppID, "release.id": f.Release.ID})
for i := 0; i < n; i++ {
job, err := f.start(name, hostID)
if err != nil {
// TODO: log/handle error
continue
}
g.Log(grohl.Data{"host.id": job.HostID, "job.id": job.ID})
}
}
示例11: OpenLogs
func (l *LibvirtLXCBackend) OpenLogs(buffers host.LogBuffers) error {
l.containersMtx.RLock()
defer l.containersMtx.RUnlock()
for id, c := range l.containers {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "OpenLogs", "job.id": id})
if err := c.followLogs(g, buffers[id]); err != nil {
return err
}
}
return nil
}
示例12: rectify
func (f *Formation) rectify() {
g := grohl.NewContext(grohl.Data{"fn": "rectify", "app.id": f.AppID, "release.id": f.Release.ID})
var hosts []*cluster.Host
if _, ok := f.c.omni[f]; ok {
hosts = f.c.hosts.List()
}
// update job counts
for t, expected := range f.Processes {
if f.Release.Processes[t].Omni {
// get job counts per host
hostCounts := make(map[string]int, len(hosts))
for _, h := range hosts {
hostCounts[h.ID()] = 0
}
for k := range f.jobs[t] {
hostCounts[k.hostID]++
}
// update per host
for hostID, actual := range hostCounts {
diff := expected - actual
g.Log(grohl.Data{"at": "update", "type": t, "expected": expected, "actual": actual, "diff": diff})
if diff > 0 {
f.add(diff, t, hostID)
} else if diff < 0 {
f.remove(-diff, t, hostID)
}
}
} else {
actual := len(f.jobs[t])
diff := expected - actual
g.Log(grohl.Data{"at": "update", "type": t, "expected": expected, "actual": actual, "diff": diff})
if diff > 0 {
f.add(diff, t, "")
} else if diff < 0 {
f.remove(-diff, t, "")
}
}
}
// remove process types
for t, jobs := range f.jobs {
// ignore one-off jobs which have no type
if t == "" {
continue
}
if _, exists := f.Processes[t]; !exists {
g.Log(grohl.Data{"at": "cleanup", "type": t, "count": len(jobs)})
f.remove(len(jobs), t, "")
}
}
}
示例13: cleanup
func (c *libvirtContainer) cleanup() error {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "cleanup", "job.id": c.job.ID})
g.Log(grohl.Data{"at": "start"})
c.unbindMounts()
if err := c.l.pinkerton.Cleanup(c.job.ID); err != nil {
g.Log(grohl.Data{"at": "pinkerton", "status": "error", "err": err})
}
if !c.job.Config.HostNetwork && c.l.bridgeNet != nil {
c.l.ipalloc.ReleaseIP(c.l.bridgeNet, c.IP)
}
g.Log(grohl.Data{"at": "finish"})
return nil
}
示例14: CloseLogs
func (l *LibvirtLXCBackend) CloseLogs() (host.LogBuffers, error) {
g := grohl.NewContext(grohl.Data{"backend": "libvirt-lxc", "fn": "CloseLogs"})
l.logStreamMtx.Lock()
defer l.logStreamMtx.Unlock()
buffers := make(host.LogBuffers, len(l.logStreams))
for id, streams := range l.logStreams {
g.Log(grohl.Data{"job.id": id})
buffer := make(host.LogBuffer, len(streams))
for fd, stream := range streams {
buffer[fd] = stream.Close()
}
buffers[id] = buffer
delete(l.logStreams, id)
}
return buffers, nil
}
示例15: restart
func (f *Formation) restart(stoppedJob *Job) error {
g := grohl.NewContext(grohl.Data{"fn": "restart", "app.id": f.AppID, "release.id": f.Release.ID})
g.Log(grohl.Data{"old.host.id": stoppedJob.HostID, "old.job.id": stoppedJob.ID})
f.jobs.Remove(stoppedJob)
var hostID string
if f.Release.Processes[stoppedJob.Type].Omni {
hostID = stoppedJob.HostID
}
newJob, err := f.start(stoppedJob.Type, hostID)
if err != nil {
return err
}
newJob.restarts = stoppedJob.restarts + 1
g.Log(grohl.Data{"new.host.id": newJob.HostID, "new.job.id": newJob.ID})
return nil
}