Skip to content

Commit

Permalink
Merge pull request #6 from Baldinof/allow-zero-replicas
Browse files Browse the repository at this point in the history
Allow to have zero replicas
  • Loading branch information
Baldinof authored May 24, 2022
2 parents b69e55a + ab62ab1 commit f5ca243
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
9 changes: 5 additions & 4 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package supervisor

import (
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"strconv"
"strings"
"time"

"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
)

func init() {
Expand Down Expand Up @@ -103,7 +104,7 @@ func parseSupervisor(d *caddyfile.Dispenser, _ interface{}) (interface{}, error)
return nil, d.Errf("'replicas' should be a positive integer, '%s' given", replicas)
}

def.Replicas = r
def.Replicas = &r
case "env":
var envKey, envValue string

Expand Down
27 changes: 24 additions & 3 deletions caddyfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package supervisor

import (
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/stretchr/testify/assert"
"path/filepath"
"reflect"
"runtime"
"testing"

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/stretchr/testify/assert"
)

func Test_parseSupervisor(t *testing.T) {
Expand Down Expand Up @@ -236,6 +237,26 @@ func Test_parseSupervisor(t *testing.T) {
}
`,
},
{
name: "replicas zero",
givenCaddyfile: `
supervisor {
php-fpm --fpm-config=fpm-8.0.2.ini {
replicas 0
}
}
`,
expectJson: `
{
"supervise": [
{
"command":["php-fpm","--fpm-config=fpm-8.0.2.ini"],
"replicas": 0
}
]
}
`,
},
{
name: "replicas wrong argument count",
givenCaddyfile: `
Expand Down
13 changes: 8 additions & 5 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package supervisor
import (
"errors"
"fmt"
"go.uber.org/zap"
"os/user"
"strings"
"time"

"go.uber.org/zap"
)

// Definition is the configuration for process to supervise
Expand All @@ -15,7 +16,7 @@ type Definition struct {
// Supports template.
Command []string `json:"command"`
// Replicas control how many instances of Command should run.
Replicas int `json:"replicas,omitempty"`
Replicas *int `json:"replicas,omitempty"`
// Dir defines the working directory the command should be executed in.
// Supports template.
// Default: current working dir
Expand Down Expand Up @@ -73,15 +74,17 @@ func (d Definition) ToSupervisors(logger *zap.Logger) ([]*Supervisor, error) {
}

if d.User != "" {
if _,err := user.Lookup(d.User); err != nil {
if _, err := user.Lookup(d.User); err != nil {
return supervisors, err
}
}

replicas := d.Replicas
var replicas int

if replicas == 0 {
if d.Replicas == nil {
replicas = 1
} else {
replicas = *d.Replicas
}

if opts.RestartPolicy == "" {
Expand Down

0 comments on commit f5ca243

Please sign in to comment.