~samwhited/blogsync

ef7e6d4c1f7f9b72b03688c2b5eb832fd38d4a31 — Sam Whited 1 year, 9 months ago 024c9d6
blogsync: wait less time for writefreely to start

Previously we would always wait 3 seconds, which may or may not be long
enough. Now we wait 5, but check every 1 second to see if we can listen
on the port. This still isn't an ideal way to do this, but it's easy and
probably "good enough".
1 files changed, 20 insertions(+), 3 deletions(-)

M preview.go
M preview.go => preview.go +20 -3
@@ 166,9 166,26 @@ https://writefreely.org/
				debug.Printf("error while executing writefreely: %v", err)
			}

			// TODO: this is jank. Manually spin up the process and wait on a log line
			// or something (which is only slightly less jank, yay shelling out).
			time.Sleep(3 * time.Second)
			// Wait until writefreely becomes available.
			var connected bool
			for i := 0; i < 5; i++ {
				const timeout = 1 * time.Second
				logger.Printf("waiting %s for writefreely to accept connections…", timeout)
				conn, err := net.Dial("tcp", net.JoinHostPort(bind, strconv.Itoa(port)))
				if err == nil {
					err = conn.Close()
					if err != nil {
						debug.Printf("error closing temporary TCP connection: %v", err)
					}
					logger.Println("connected to writefreely!")
					connected = true
					break
				}
				time.Sleep(timeout)
			}
			if !connected {
				return fmt.Errorf("failed to connect to writefreely, did it start?")
			}

			client := writeas.NewClientWith(writeas.Config{
				URL: "http://" + net.JoinHostPort(bind, strconv.Itoa(port)+"/api"),