Compare commits
2
Commits
5b25695954
...
7e4686a229
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e4686a229 | ||
|
|
618574f160 |
@@ -6,11 +6,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"sync"
|
||||||
|
|
||||||
"git.stephensearles.com/stephen/acedoc"
|
"git.stephensearles.com/stephen/acedoc"
|
||||||
|
"git.stephensearles.com/stephen/idleshut"
|
||||||
)
|
)
|
||||||
|
|
||||||
type editSession struct {
|
type editSession struct {
|
||||||
@@ -19,6 +19,14 @@ type editSession struct {
|
|||||||
filename string
|
filename string
|
||||||
doc *acedoc.Document
|
doc *acedoc.Document
|
||||||
tmpdir string
|
tmpdir string
|
||||||
|
mtx sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (es *editSession) Clients() uint {
|
||||||
|
es.mtx.Lock()
|
||||||
|
defer es.mtx.Unlock()
|
||||||
|
|
||||||
|
return es.clients
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||||
@@ -54,61 +62,15 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ch *CaddyHugo) renderDraft(es *editSession) error {
|
func (ch *CaddyHugo) renderDraft(es *editSession) error {
|
||||||
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir)
|
var proc *idleshut.Process
|
||||||
hugoCmd.Dir = ch.Dir
|
|
||||||
err := hugoCmd.Start()
|
f := func() {
|
||||||
if err != nil {
|
if es.clients != 0 {
|
||||||
return fmt.Errorf("error starting hugo: %v", err)
|
proc.Touch()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
proc = idleshut.New(HugoCmdProcessConfig(ch, es, f))
|
||||||
ticker := time.NewTicker(WebsocketFileTicker)
|
|
||||||
idleTicks := 0
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
err := hugoCmd.Process.Signal(os.Interrupt)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error signaling to hugo:", err)
|
|
||||||
}
|
|
||||||
err = hugoCmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error waiting for hugo:", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
<-ticker.C
|
|
||||||
ch.mtx.Lock()
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error saving document contents:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if es.clients == 0 {
|
|
||||||
idleTicks++
|
|
||||||
idleTime := time.Duration(idleTicks) * WebsocketFileTicker
|
|
||||||
if idleTime >= IdleWebsocketTimeout {
|
|
||||||
err := ch.Publish()
|
|
||||||
fmt.Printf("idle for %v, quitting\n", idleTime)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf(", error publishing: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
es.doc.Close()
|
|
||||||
os.RemoveAll(es.tmpdir)
|
|
||||||
delete(ch.docs, es.filename)
|
|
||||||
ch.mtx.Unlock()
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
fmt.Println("idle for", idleTime)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
idleTicks = 0
|
|
||||||
}
|
|
||||||
ch.mtx.Unlock()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package caddyhugo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"git.stephensearles.com/stephen/idleshut"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HugoInteractor interface {
|
||||||
|
Render(srcdir, workdir string) HugoRenderer
|
||||||
|
}
|
||||||
|
|
||||||
|
type HugoRenderer interface {
|
||||||
|
WriteContent(contents string) error
|
||||||
|
Start() error
|
||||||
|
Stop() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) idleshut.Config {
|
||||||
|
cmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir)
|
||||||
|
cmd.Dir = es.tmpdir
|
||||||
|
|
||||||
|
return idleshut.Config{
|
||||||
|
Tick: WebsocketFileTicker,
|
||||||
|
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1,
|
||||||
|
Start: func() error {
|
||||||
|
|
||||||
|
err := cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error starting hugo: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Stop: func() error {
|
||||||
|
if cmd == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cmd.Process.Signal(os.Interrupt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error signalling hugo to stop: %v", err)
|
||||||
|
}
|
||||||
|
err = cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error waiting for hugo to stop: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
es.doc.Close()
|
||||||
|
os.RemoveAll(es.tmpdir)
|
||||||
|
delete(ch.docs, es.filename)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
IdleProcessError: func(err error) {
|
||||||
|
fmt.Println("error processing draft:", err)
|
||||||
|
},
|
||||||
|
IdleTick: func() error {
|
||||||
|
return ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644)
|
||||||
|
},
|
||||||
|
ActiveTick: func() error {
|
||||||
|
return ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user