Skip to content

Commit

Permalink
alerts memcache server
Browse files Browse the repository at this point in the history
  • Loading branch information
v00g100skr committed Nov 28, 2023
1 parent 6f8498a commit 102ee68
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions deploy/tcp_server/DockerfileCheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.10.6
ADD check.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
CMD python check.py
50 changes: 50 additions & 0 deletions deploy/tcp_server/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json
import os
import asyncio
from aiomcache import Client

memcached_host = os.environ.get('MEMCACHED_HOST') or 'localhost'

alert_loop_time = os.environ.get('ALERT_PERIOD') or 3


async def alarm_data(mc, alerts_cached_data):
try:
task1_result = await mc.get(b"alerts")
print(f"task result")

if task1_result:
encoded_result = json.loads(task1_result.decode('utf-8'))

await asyncio.sleep(alert_loop_time)

except Exception as e:
print(f"Error fetching data: {str(e)}")
raise


async def main():
mc = Client(memcached_host, 11211) # Connect to your Memcache server
alerts_cached_data = {}
while True:
try:
print(f"task start")
await alarm_data(mc, alerts_cached_data)

except asyncio.CancelledError:
print("Task canceled. Shutting down...")
await mc.close()
break

except Exception as e:
print(f"Caught an exception: {e}")

finally:
asyncio.sleep(1)

if __name__ == "__main__":
try:
print("Start")
asyncio.run(main())
except KeyboardInterrupt:
pass
54 changes: 54 additions & 0 deletions deploy/tcp_server/redeploy_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Default values
ALERT_TOKEN=""
MEMCACHED_HOST=""

# Check for arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--alert-token)
ALERT_TOKEN="$2"
shift 2
;;
-m|--memcached-host)
MEMCACHED_HOST="$2"
shift 2
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done

echo "ALERTS"

echo "ALERT_TOKEN: $ALERT_TOKEN"
echo "MEMCACHED_HOST: $MEMCACHED_HOST"


# Updating the Git repo
echo "Updating Git repo..."
#cd /path/to/your/git/repo
git pull

# Moving to the deployment directory
echo "Moving to deployment directory..."
#cd /deploy/tcp_server

# Building Docker image
echo "Building Docker image..."
docker build -t check -f DockerfileCheck .

# Stopping and removing the old container (if exists)
echo "Stopping and removing old container..."
docker stop check || true
docker rm check || true

# Deploying the new container
echo "Deploying new container..."
docker run --name check --restart unless-stopped -d --env MEMCACHED_HOST="$MEMCACHED_HOST" check

echo "Container deployed successfully!"

0 comments on commit 102ee68

Please sign in to comment.