a lightweight dependency resolver
Linker is a lightweight dependency resolver
gcc linker.c -lpthread -o linker
$ linker -h
linker [opt] [event:script].. [service:event]..
opt:
-h : print help
-r <role> : set role [deamon/waitfor]
-p <port> : listen port
-P <peerport> : peer port (port to send request/response)
example:
./linker -p 5000 -r deamon mysql_start:/usr/local/mysql_start_check.sh
./linker -p 4000 -P 5000 -r waitfor mysql:mysql_start
Linker use two roles:
1. Daemon
2. waitfor
While running as daemon linker runs in background, to perform event check requests. On a event check request from dependent node, it executes corresponsing event check script and return response
While running as waitfor linker runs in foreground, to halt execution till all the event has successfully occured
linker can be useful in a docker compose environment. Docker entrypoint script can use linker to waitfor other service in different container
For a situation where webserver
container depends on mysql
container, we can use docker compose features to link
and depends_on
. Although depends_on
check if the container has started, but not the application status
to halt webserver
from starting, linter
can be used to waitfor mysql
to start
to check mysql
status locally, linker deamon can be started in mysql
container as:
linker -r deamon -p 5001 -P 5000 mysql_start:./mysql_start_check.sh
Here we registering mysql_start_check.sh
script for mysql_start
eventmysql_start_check.sh
should return zero if mysql is started, and non zero in case of errormysql_start_check.sh
script can be written as:
#!/bin/bash
USER=root
PASS=root123
mysqladmin -h remote_server_ip -u$USER -p$PASS processlist
if [ $? -eq 0 ]
then
echo "do nothing"
else
ssh remote_server_ip
service mysqld start
fi
to make webserver
container wait for mysql to start, linker can be started to waitfor in the entrypoint script:
linker -r waitfor -p 5000 -P 5001 mysql:mysql_start
A service name can be used, which would be resolved dynamically if the containers are linked
In a daemon multiple events check script can be resgisterd
For example in db
service:
linker -r deamon -p 5001 -P 5000 mysql_start:./mysql_start_check.sh pesql_start:./pesql_start_check.sh
At the same way, While waiting for multiple events can be resgisterd as
linker -r waitfor -p 5000 -P 5001 db:mysql_start db:pesql_start