mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-15 11:52:14 +01:00
adding new attribute lock (#8)
This commit is contained in:
@@ -14,6 +14,11 @@ logs:
|
|||||||
# You may provide here your own implementation!
|
# You may provide here your own implementation!
|
||||||
commands:
|
commands:
|
||||||
|
|
||||||
|
# Some libraries like unoconv cannot perform concurrent conversions. That's why the API does only one conversion at a time.
|
||||||
|
# If your current implementation uses libraries which are able to perform concurrent conversions, you may
|
||||||
|
# change this value to false.
|
||||||
|
lock: true
|
||||||
|
|
||||||
# Unlike others commands' templates, you have access to FilesPaths instead of FilePath: it gathers all PDF files which should be merged.
|
# Unlike others commands' templates, you have access to FilesPaths instead of FilePath: it gathers all PDF files which should be merged.
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ logs:
|
|||||||
...
|
...
|
||||||
}
|
}
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
35
_tests/configurations/no-lock-gotenberg.yml
Normal file
35
_tests/configurations/no-lock-gotenberg.yml
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
port: 3000
|
||||||
|
logs:
|
||||||
|
level: "DEBUG"
|
||||||
|
formatter: "text"
|
||||||
|
commands:
|
||||||
|
lock: false
|
||||||
|
merge:
|
||||||
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
|
interpreter: "/bin/sh -c"
|
||||||
|
timeout: 30
|
||||||
|
conversions:
|
||||||
|
- template: "markdown-pdf {{ .FilePath }} -o {{ .ResultFilePath }}"
|
||||||
|
interpreter: "/bin/sh -c"
|
||||||
|
timeout: 30
|
||||||
|
extensions:
|
||||||
|
- ".md"
|
||||||
|
- template: "xvfb-run -e /dev/stdout wkhtmltopdf {{ .FilePath }} {{ .ResultFilePath }}"
|
||||||
|
interpreter: "/bin/sh -c"
|
||||||
|
timeout: 30
|
||||||
|
extensions:
|
||||||
|
- ".html"
|
||||||
|
- ".htm"
|
||||||
|
- template: "unoconv --format pdf --output \"{{ .ResultFilePath }}\" \"{{ .FilePath }}\""
|
||||||
|
interpreter: "/bin/sh -c"
|
||||||
|
timeout: 30
|
||||||
|
extensions:
|
||||||
|
- ".doc"
|
||||||
|
- ".docx"
|
||||||
|
- ".odt"
|
||||||
|
- ".xls"
|
||||||
|
- ".xlsx"
|
||||||
|
- ".ods"
|
||||||
|
- ".ppt"
|
||||||
|
- ".pptx"
|
||||||
|
- ".odp"
|
||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "DEBUG"
|
formatter: "DEBUG"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "text"
|
level: "text"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := .FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ logs:
|
|||||||
level: "DEBUG"
|
level: "DEBUG"
|
||||||
formatter: "text"
|
formatter: "text"
|
||||||
commands:
|
commands:
|
||||||
|
lock: true
|
||||||
merge:
|
merge:
|
||||||
template: "pdftk {{ range $filePath := FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
template: "pdftk {{ range $filePath := FilesPaths }} {{ $filePath }} {{ end }} cat output {{ .ResultFilePath }}"
|
||||||
interpreter: "/bin/sh -c"
|
interpreter: "/bin/sh -c"
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type (
|
|||||||
port string
|
port string
|
||||||
logsLevel logrus.Level
|
logsLevel logrus.Level
|
||||||
logsFormatter logrus.Formatter
|
logsFormatter logrus.Formatter
|
||||||
|
lock bool
|
||||||
// commands associates a file extension with a Command instance.
|
// commands associates a file extension with a Command instance.
|
||||||
// Particular case: ".pdf" extension is used for the merge command.
|
// Particular case: ".pdf" extension is used for the merge command.
|
||||||
commands map[string]*Command
|
commands map[string]*Command
|
||||||
@@ -126,6 +127,16 @@ func GetLogsFormatter() logrus.Formatter {
|
|||||||
return config.logsFormatter
|
return config.logsFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithLock sets the lock strategy.
|
||||||
|
func WithLock(lock bool) {
|
||||||
|
config.lock = lock
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasLock returns the current lock strategy.
|
||||||
|
func HasLock() bool {
|
||||||
|
return config.lock
|
||||||
|
}
|
||||||
|
|
||||||
type interpreterEmptyError struct {
|
type interpreterEmptyError struct {
|
||||||
command string
|
command string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,24 @@ func TestGetLogsFormatter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithLock(t *testing.T) {
|
||||||
|
lock := true
|
||||||
|
WithLock(lock)
|
||||||
|
|
||||||
|
if config.lock != lock {
|
||||||
|
t.Errorf("Configuration populated with a wrong lock strategy: got '%t' want '%t'", config.lock, lock)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasLock(t *testing.T) {
|
||||||
|
lock := true
|
||||||
|
config.lock = true
|
||||||
|
|
||||||
|
if HasLock() != lock {
|
||||||
|
t.Errorf("Configuration returned a wrong port: got '%t' want '%t'", HasLock(), lock)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInterpreterEmptyError(t *testing.T) {
|
func TestInterpreterEmptyError(t *testing.T) {
|
||||||
err := &interpreterEmptyError{"echo hello world"}
|
err := &interpreterEmptyError{"echo hello world"}
|
||||||
expected := fmt.Sprintf(interpreterEmptyErrorMessage, err.command)
|
expected := fmt.Sprintf(interpreterEmptyErrorMessage, err.command)
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ func ParseFile(configurationFilePath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WithLock(fileConfig.Commands.Lock)
|
||||||
|
|
||||||
// handles merge command first...
|
// handles merge command first...
|
||||||
cmd, err := NewCommand(fileConfig.Commands.Merge.Template, fileConfig.Commands.Merge.Interpreter, fileConfig.Commands.Merge.Timeout)
|
cmd, err := NewCommand(fileConfig.Commands.Merge.Template, fileConfig.Commands.Merge.Interpreter, fileConfig.Commands.Merge.Timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -57,6 +59,7 @@ type (
|
|||||||
Formatter string `yaml:"formatter"`
|
Formatter string `yaml:"formatter"`
|
||||||
} `yaml:"logs"`
|
} `yaml:"logs"`
|
||||||
Commands struct {
|
Commands struct {
|
||||||
|
Lock bool `yaml:"lock"`
|
||||||
Merge *mergeCommand `yaml:"merge"`
|
Merge *mergeCommand `yaml:"merge"`
|
||||||
Conversions []*conversionCommand `yaml:"conversions,omitempty"`
|
Conversions []*conversionCommand `yaml:"conversions,omitempty"`
|
||||||
} `yaml:"commands"`
|
} `yaml:"commands"`
|
||||||
|
|||||||
@@ -33,8 +33,12 @@ func (e *commandTimeoutError) Error() string {
|
|||||||
// run runs the given command. If timeout is reached or
|
// run runs the given command. If timeout is reached or
|
||||||
// something bad happened, returns an error.
|
// something bad happened, returns an error.
|
||||||
func (r *runner) run(command string, interpreter []string, timeout int) error {
|
func (r *runner) run(command string, interpreter []string, timeout int) error {
|
||||||
r.mu.Lock()
|
if config.HasLock() {
|
||||||
defer r.mu.Unlock()
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
} else {
|
||||||
|
logger.Warn("lock disabled")
|
||||||
|
}
|
||||||
|
|
||||||
binary := interpreter[0]
|
binary := interpreter[0]
|
||||||
parameters := append(interpreter[1:], command)
|
parameters := append(interpreter[1:], command)
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ func TestCommandTimeoutError(t *testing.T) {
|
|||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
var cmd string
|
var cmd string
|
||||||
|
|
||||||
|
load("../../../_tests/configurations/gotenberg.yml")
|
||||||
|
|
||||||
// case 1: uses a simple command.
|
// case 1: uses a simple command.
|
||||||
cmd = "echo Hello world"
|
cmd = "echo Hello world"
|
||||||
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err != nil {
|
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err != nil {
|
||||||
@@ -58,6 +60,14 @@ func TestRun(t *testing.T) {
|
|||||||
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err == nil {
|
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err == nil {
|
||||||
t.Errorf("Command '%s' should not have worked", cmd)
|
t.Errorf("Command '%s' should not have worked", cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
load("../../../_tests/configurations/no-lock-gotenberg.yml")
|
||||||
|
|
||||||
|
// case 4: uses a configuration with a no lock strategy.
|
||||||
|
cmd = "echo Hello world"
|
||||||
|
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err != nil {
|
||||||
|
t.Errorf("Command '%s' should have worked", cmd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnconv(t *testing.T) {
|
func TestUnconv(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user