func BackupAction(c *cli.Context) { CheckUid(c.Command.Name) a := getAgent(c) if err := a.TrackMetrics(); err != nil { fmt.Println(err) } if _, err := lock.Lock(LockPath); err != nil { log.Fatal(err) } if err := a.RunTasks(); err != nil { log.Fatal(err) } else { fmt.Println("Completed!") } }
func lockFile(path string) (io.Closer, error) { retry := 100 * time.Microsecond startTime := time.Now() for { if locker, err := filelock.Lock(path); err == nil { return locker, nil } total := time.Since(startTime) if total > maxRetryDuration { return nil, errgo.New("file locked for too long; giving up") } // Always have at least one try at the end of the interval. if remain := maxRetryDuration - total; retry > remain { retry = remain } time.Sleep(retry) retry *= 2 } }
func (s *Server) Listen(addr string) error { s.Info("Initializing state dir", "dir", s.stateDir) if err := os.MkdirAll(string(s.stateDir), 0755); err != nil { return err } closer, err := lock.Lock(s.stateDir.LockFile()) if err != nil { return err } defer closer.Close() c := Config{ Addr: addr, } if err := writeConfigFile(c, s.stateDir); err != nil { return err } s.Info("Registering plugins") s.plugins = listAllPlugins() for _, p := range s.plugins { s.Info("Plugin found", "plugin", p.name, "path", p.path) if err := p.initialize(s.stateDir); err != nil { return err } s.registerRoutes(p.Routes()) } return s.Server.Listen(addr) }
func newFileFromOSFile(f lldb.OSFile) (fi *file, err error) { nm := lockName(f.Name()) lck, err := lock.Lock(nm) if err != nil { if lck != nil { lck.Close() } return nil, err } close := true defer func() { if close && lck != nil { lck.Close() } }() var w *os.File closew := false wn := walName(f.Name()) w, err = os.OpenFile(wn, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) closew = true defer func() { if closew { nm := w.Name() w.Close() os.Remove(nm) w = nil } }() if err != nil { if !os.IsExist(err) { return nil, err } closew = false w, err = os.OpenFile(wn, os.O_RDWR, 0666) if err != nil { return nil, err } closew = true st, err := w.Stat() if err != nil { return nil, err } if st.Size() != 0 { return nil, fmt.Errorf("non empty WAL file %s exists", wn) } } info, err := f.Stat() if err != nil { return nil, err } switch sz := info.Size(); { case sz == 0: b := make([]byte, 16) copy(b, []byte(magic)) if _, err := f.Write(b); err != nil { return nil, err } filer := lldb.Filer(lldb.NewOSFiler(f)) filer = lldb.NewInnerFiler(filer, 16) if filer, err = lldb.NewACIDFiler(filer, w); err != nil { return nil, err } a, err := lldb.NewAllocator(filer, &lldb.Options{}) if err != nil { return nil, err } a.Compress = true s := &file{ a: a, codec: newGobCoder(), f0: f, f: filer, lck: lck, name: f.Name(), wal: w, } if err = s.BeginTransaction(); err != nil { return nil, err } h, err := s.Create() if err != nil { return nil, err } if h != 1 { // root log.Panic("internal error") } if h, err = s.a.Alloc(make([]byte, 8)); err != nil { return nil, err } if h != 2 { // id log.Panic("internal error") } close, closew = false, false return s, s.Commit() default: b := make([]byte, 16) if _, err := f.Read(b); err != nil { return nil, err } if string(b[:len(magic)]) != magic { return nil, fmt.Errorf("unknown file format") } filer := lldb.Filer(lldb.NewOSFiler(f)) filer = lldb.NewInnerFiler(filer, 16) if filer, err = lldb.NewACIDFiler(filer, w); err != nil { return nil, err } a, err := lldb.NewAllocator(filer, &lldb.Options{}) if err != nil { return nil, err } bid, err := a.Get(nil, 2) // id if err != nil { return nil, err } if len(bid) != 8 { return nil, fmt.Errorf("corrupted id |% x|", bid) } id := int64(0) for _, v := range bid { id = (id << 8) | int64(v) } a.Compress = true s := &file{ a: a, codec: newGobCoder(), f0: f, f: filer, id: id, lck: lck, name: f.Name(), wal: w, } close, closew = false, false return s, nil } }
func Lock(confdir string) (io.Closer, error) { c, err := lock.Lock(path.Join(confdir, LockFile)) return c, err }