Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This mod gives SWAG the ability to start containers on-demand when accessed thro
![loading-page](.assets/loading-page.png)

Instead of showing a 502 error page, it can display a loading page and auto-refresh once the container is up.

Add the following `include` to each proxy-conf where you wish to show the loading page inside the `server` section:
```nginx
server {
Expand Down Expand Up @@ -68,7 +68,7 @@ Or set the following label if using `swag-auto-proxy`:
```
### Labels:
- `swag_ondemand=enable` - required for on-demand.
- `swag_ondemand_urls=https://wake.domain.com,https://app.domain.com/up` - *optional* - overrides the monitored URLs for starting the container on-demand. Defaults to `https://somecontainer.,http://somecontainer.`.
- `swag_ondemand_urls=https://wake.domain.com,https://app.domain.com/up` - *optional* - overrides the monitored URLs for starting the container on-demand. Defaults to using the value of the `swag_url` label, if you've already set it for `swag-auto-proxy`, or `https://somecontainer.,http://somecontainer.` otherwise.

### URLs:
- Accessed URLs need to start with one of `swag_ondemand_urls` to be matched, for example, setting `swag_ondemand_urls=https://plex.` will apply to `https://plex.domain.com` and `https://plex.domain.com/something`.
Expand Down
21 changes: 12 additions & 9 deletions root/app/swag-ondemand.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
self.daemon = True
self.ondemand_containers = {}
self.init_docker()

def init_docker(self):
try:
docker_host = os.environ.get("DOCKER_HOST", None)
Expand All @@ -37,20 +37,23 @@ def init_docker(self):
def process_containers(self):
containers = self.docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] })
container_names = {container.name for container in containers}

for container_name in list(self.ondemand_containers.keys()):
if container_name in container_names:
continue
self.ondemand_containers.pop(container_name)
logging.info(f"Stopped monitoring {container_name}")

for container in containers:
container_urls = container.labels.get("swag_ondemand_urls", f"https://{container.name}.,http://{container.name}.")
default_url = container.labels.get("swag_url", f"{container.name}.").rstrip("*")
container_urls = container.labels.get("swag_ondemand_urls", f"https://{default_url},http://{default_url}")
if container.name not in self.ondemand_containers.keys():
last_accessed = datetime.now()
logging.info(f"Started monitoring {container.name}")
logging.info(f"Started monitoring {container.name} for urls: {container_urls}")
else:
last_accessed = self.ondemand_containers[container.name]["last_accessed"]
if container_urls != self.ondemand_containers[container.name]["urls"]:
logging.info(f"Updated urls for {container.name} to: {container_urls}")
self.ondemand_containers[container.name] = { "status": container.status, "urls": container_urls, "last_accessed": last_accessed }

def stop_containers(self):
Expand All @@ -62,12 +65,12 @@ def stop_containers(self):
continue
self.docker_client.containers.get(container_name).stop()
logging.info(f"Stopped {container_name} after {STOP_THRESHOLD}s of inactivity")

def start_containers(self):
with last_accessed_urls_lock:
last_accessed_urls_combined = ",".join(last_accessed_urls)
last_accessed_urls.clear()

for container_name in self.ondemand_containers.keys():
accessed = False
for ondemand_url in self.ondemand_containers[container_name]["urls"].split(","):
Expand Down Expand Up @@ -95,7 +98,7 @@ class LogReaderThread(threading.Thread):
def __init__(self):
super().__init__()
self.daemon = True

def tail(self, f):
f.seek(0,2)
inode = os.fstat(f.fileno()).st_ino
Expand Down Expand Up @@ -143,6 +146,6 @@ def run(self):

ContainerThread().start()
LogReaderThread().start()

while True:
time.sleep(1)