update 0.1.2

This commit is contained in:
ElevenNotes
2023-12-04 13:47:27 +01:00
parent 0259df47ef
commit 2076495de5
2 changed files with 19 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
# Alpine :: Traefik Labels
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.1?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.2?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
Run Traefik Labels based on Alpine Linux. Small, lightweight, secure and fast 🏔️

View File

@@ -1,5 +1,6 @@
const Docker = require('dockerode');
const redis = require('redis');
const ERROR = 1;
class Labels{
#docker;
@@ -10,6 +11,14 @@ class Labels{
this.#docker = new Docker({socketPath: '/run/docker.sock'});
}
#log(message, error){
console.log(JSON.stringify({
time:new Date().toISOString(),
type:(error === ERROR) ? 'ERROR' : 'INFO',
message:message,
}));
}
async watch(){
this.#redis = await redis.createClient({
url:process.env.LABELS_REDIS_URL,
@@ -21,20 +30,20 @@ class Labels{
this.#redis.connect();
this.#redis.on('ready', ()=>{
this.#log('connected to Redis');
(async() => {
await this.dockerPoll();
})();
this.dockerEvents();
});
this.#redis.on('error', error =>{
console.error(error);
this.#log(error, ERROR);
});
setInterval(async() => {
await this.dockerPoll();
}, parseInt(process.env.LABELS_INTERVAL)*1000);
(async() => {
await this.dockerPoll();
})();
}
dockerEvents(){
@@ -42,6 +51,7 @@ class Labels{
data.on('data', async(chunk) => {
const event = JSON.parse(chunk.toString('utf8'));
if(/Container/i.test(event?.Type) && /start|stop|restart|kill|die|destroy/i.test(event?.status)){
this.#log(`new docker event for container ${event.id}`);
await this.dockerInspect(event.id, event.status);
}
});
@@ -52,14 +62,16 @@ class Labels{
if(!this.#poll){
try{
this.#poll = true;
this.#log(`poll start (interval:${process.env.LABELS_INTERVAL})}`);
this.#docker.listContainers((error, containers) => {
containers.forEach(async(container) => {
await this.dockerInspect(container.Id, 'start');
});
});
}catch(e){
console.error(e);
this.#log(e, ERROR);
}finally{
this.#log('poll end');
this.#poll = false;
}
}