Skip to content

Commit

Permalink
Add projects autostart switch (#63)
Browse files Browse the repository at this point in the history
* Add autostart projects switch

* Update Dockerfile with PROJECT_AUTOSTART_DISABLE default value

* Make env variable available in proxyctl. Make lua functions local (prevent warnings in vhost-proxy log)

* Rename variable PROJECT_AUTOSTART_DISABLE to PROJECT_AUTOSTART

* Update README.md

* Remove unnecessary code

* Make env variables available in proxyctl started from proxyctl.lua

Co-authored-by: Leonid Makarov <leonid.makarov@ffwagency.com>
  • Loading branch information
lmakarov authored Mar 4, 2020
1 parent d17214b commit 62e5991
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ ENV \
PROJECT_INACTIVITY_TIMEOUT=0 \
# Disable DANGLING_TIMEOUT by default
PROJECT_DANGLING_TIMEOUT=0 \
# Enable PROJECT_AUTOSTART by default
PROJECT_AUTOSTART=1 \
# Disable access log by default
ACCESS_LOG=0 \
# Disable debug output by default
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ docker run -d --name=nodejs \
## Advanced proxy configuration

These advanced settings can be used in CI sandbox environments and help keep the resource usage down by stopping
Docksal project containers after a period of inactivity. Projects are automatically restarting upon a new HTTP request.
Docksal project containers after a period of inactivity. Projects are automatically restarted upon a new HTTP request (unless `PROJECT_AUTOSTART` is set to `0`, see below.).

`PROJECT_INACTIVITY_TIMEOUT`

Expand Down Expand Up @@ -107,6 +107,9 @@ environment specific equivalent, e.g. `docksal-ci.env`).

Note: permanent projects will still be put into hibernation according to `PROJECT_INACTIVITY_TIMEOUT`.

`PROJECT_AUTOSTART`

Setting this variable to `0` will disable autostart projects by visiting project url. This option is active by default (set to `1`).

## Default and custom certs for HTTPS

Expand Down
3 changes: 3 additions & 0 deletions bin/proxyctl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ start ()
# Set logging prefix for the function and trigger a log entry
export LOG_PREFIX='[start]' && log

# Disable projects autostart in case PROJECT_AUTOSTART set to 0
[[ "$PROJECT_AUTOSTART" == 0 ]] && log "WARNING: Projects autostart is disabled." && return 0

local type=${1}
local id=${2}

Expand Down
8 changes: 4 additions & 4 deletions conf/nginx/lua/proxyctl.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- Debug pring helper
function dpr(message)
local function dpr(message)
if (tonumber(os.getenv("DEBUG_LOG")) > 0) then
ngx.log(ngx.STDERR, "DEBUG: " .. message)
end
end

-- Helper function to read a file from disk
function read_file(path)
local function read_file(path)
local open = io.open
local file = open(path, "rb") -- r read mode and b binary mode
if not file then return nil end
Expand All @@ -16,7 +16,7 @@ function read_file(path)
end

-- Returns loading.html for HTTP_OK and not-found.html otherwise
function response(status)
local function response(status)
-- Load response body from disk
-- This used to be done with ngx.location.capture, which does not support HTTP/2 and fails with https requests:
-- See https://github.com/openresty/lua-nginx-module/issues/1285#issuecomment-376418678
Expand Down Expand Up @@ -62,7 +62,7 @@ if (lock_timestamp == 0) then

-- Lanch project start script
-- os.execute returs multiple values starting with Lua 5.2
local status, exit, exit_code = os.execute("sudo /usr/local/bin/proxyctl start $(sudo /usr/local/bin/proxyctl lookup \"" .. ngx.var.host .. "\")")
local status, exit, exit_code = os.execute("sudo -E /usr/local/bin/proxyctl start $(sudo /usr/local/bin/proxyctl lookup \"" .. ngx.var.host .. "\")")

if (exit_code == 0) then
-- If all went well, reload the page
Expand Down
3 changes: 3 additions & 0 deletions conf/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pid /var/run/nginx.pid;
daemon off;

env DEBUG_LOG;
env PROJECT_INACTIVITY_TIMEOUT;
env PROJECT_DANGLING_TIMEOUT;
env PROJECT_AUTOSTART;

# Send logs to stderr
error_log /dev/stderr warn;
Expand Down

0 comments on commit 62e5991

Please sign in to comment.