Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
robo-monk committed Dec 10, 2024
1 parent 1e5148d commit 4cc57e2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
60 changes: 32 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# lid

Lid is a very simple process manager written in Go. It is a lightweight alternative to pm2 and forever.
It is inspired by the fantastic DX of [Pocketbase](https://pocketbase.io/) and follows similar patterns.
Lid is a very simple process manager inspired by pocketbase and pm2.
I use it in a monorepo to orchestrate various processes in production.

It aims to have:
- **No Background Daemon**
- **Simple Interface**
- **Configurable Behavior**
- **Zero Downtime**
- **Agnostic Process Support**
- No background deamon
- Very simple interface
- Callbacks when processes exit
- Zero(ish?) downtime when restarting applications

> This project is in alpha. Use with caution.
> young code use with care
### Recommended Installation
1. Create a `lid` directory in the root of your monorepo
2. Create a `lid/lid.go` and register your services
3. `go mod init lid && go mod tidy && go build -o lid`
4. Add `lid/lid.log` and `lid/lid` to `.gitignore`
2. Create a `lid/lig.go` and register your services
3. `go mod init lig && go mod tidy && go build -o lid`

### CLI Usage

Expand All @@ -36,9 +34,7 @@ Available commands:
list Lists the status of all services

Available services:
- pocketbase
- backend
- frontend
...
```

### Example Configuration
Expand All @@ -56,30 +52,38 @@ func main() {
manager := lid.New()

manager.Register("pocketbase", &lid.Service{
Cwd: "../pocketbase",
Command: []string{"./pocketbase", "serve"},
EnvFile: ".env",
OnExit: func(e *exec.ExitError, service *lid.Service) {
service.Logger.Println("pocketbase failed")
// ... log the error further
// Restart the service
// the cwd is ALWAYS relative to the executable
Cwd: "../pocketbase",
Command: []string{"./pocketbase", "serve"},

// Env file relative to Cwd
EnvFile: ".env",
OnExit: func(e *exec.ExitError, service *lid.Service) {
service.Logger.Println("pocketbase failed")

// ... log the error further

// Restart the service
service.Start()
},
})

manager.Register("backend", &lid.Service {
Cwd: "../server"
EnvFile: ".production.env"
Command: []string{"./dist/server"},
OnExit: func(e *exec.ExitError, service *lid.Service) {
// the cwd is ALWAYS relative to the executable
Cwd: "../server",
// Env file relative to Cwd
EnvFile: ".production.env",
Command: []string{"./dist/server"},
OnExit: func(e *exec.ExitError, service *lid.Service) {
service.Start()
},
})

manager.Register("frontend", &lid.Service {
Cwd: "../frontend"
Command: []string{ "pnpm", "run", "start" },
OnExit: func(e *exec.ExitError, service *lid.Service) {
// the cwd is ALWAYS relative to the executable
Cwd: "../frontend",
Command: []string{ "pnpm", "run", "start" },
OnExit: func(e *exec.ExitError, service *lid.Service) {
service.Start()
},
})
Expand Down
32 changes: 32 additions & 0 deletions lid/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lid

import (
"fmt"
"os"
"path/filepath"
)


func getExecutableDir() (string, error) {
// Get the path to the executable
execPath, err := os.Executable()
if err != nil {
return "", fmt.Errorf("failed to get executable path: %w", err)
}

// Get the directory of the executable
execDir := filepath.Dir(execPath)
return execDir, nil
}

func getRelativePath(relativePath string) (string, error) {
// Get the executable's directory
execDir, err := getExecutableDir()
if err != nil {
return "", err
}

// Join the executable's directory with the relative path
fullPath := filepath.Join(execDir, relativePath)
return fullPath, nil
}
6 changes: 3 additions & 3 deletions lid/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ type Service struct {
Command []string
EnvFile string

// OnStop func(e *exec.ExitError, self *Service)
OnExit func(e *exec.ExitError, self *Service)
}

Expand Down Expand Up @@ -102,12 +101,13 @@ func (s *Service) PrepareCommand() (*exec.Cmd, error) {
cmd := exec.Command(s.Command[0], s.Command[1:]...)

if s.Cwd != "" {
cmd.Dir, _ = filepath.Abs(s.Cwd)
cmd.Dir, _ = getRelativePath(s.Cwd)
}

if s.EnvFile != "" {
var ferr error
cmd.Env, ferr = ReadDotEnvFile(filepath.Join(s.Cwd, s.EnvFile))
envPath, _ := getRelativePath(filepath.Join(s.Cwd, s.EnvFile))
cmd.Env, ferr = ReadDotEnvFile(envPath)
if ferr != nil{
return nil, ferr
}
Expand Down

0 comments on commit 4cc57e2

Please sign in to comment.