add automatic transferscript
This commit is contained in:
parent
ee414d4b57
commit
3531681e7e
21
remotenzb/api.py
Normal file
21
remotenzb/api.py
Normal file
@ -0,0 +1,21 @@
|
||||
from fastapi import FastAPI
|
||||
from state import state, state_lock
|
||||
import uvicorn
|
||||
|
||||
|
||||
def create_api():
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/status")
|
||||
def get_status():
|
||||
|
||||
with state_lock:
|
||||
return state
|
||||
|
||||
return app
|
||||
|
||||
def start_api():
|
||||
app = create_api()
|
||||
uvicorn.run(app, host="127.0.0.1", port=8000)
|
||||
|
||||
|
||||
2
remotenzb/config.py
Normal file
2
remotenzb/config.py
Normal file
@ -0,0 +1,2 @@
|
||||
WATCH_PATH = "/root/remotenzb"
|
||||
REMOTE = "/root/test"
|
||||
48
remotenzb/main.py
Normal file
48
remotenzb/main.py
Normal file
@ -0,0 +1,48 @@
|
||||
from watcher import start_watcher
|
||||
from time import sleep
|
||||
from state import state, state_lock
|
||||
from transfer import run_transfer
|
||||
from config import WATCH_PATH
|
||||
from api import start_api
|
||||
import logging
|
||||
import threading
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
def worker():
|
||||
while True:
|
||||
with state_lock:
|
||||
if not state["running"] and state["queue"]:
|
||||
directory = state["queue"].pop(0)
|
||||
else:
|
||||
directory = None
|
||||
|
||||
if directory:
|
||||
logger.info(f"Starting transfer for: {directory}")
|
||||
run_transfer(directory)
|
||||
else:
|
||||
sleep(1)
|
||||
|
||||
def main():
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(asctime)s - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
observer = start_watcher(WATCH_PATH)
|
||||
t = threading.Thread(target=worker, daemon=True)
|
||||
t.start()
|
||||
logger.info("Watcher and worker started.")
|
||||
|
||||
|
||||
api_thread = threading.Thread(target=start_api, daemon=True)
|
||||
api_thread.start()
|
||||
|
||||
|
||||
try:
|
||||
while True:
|
||||
sleep(5)
|
||||
except KeyboardInterrupt:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
|
||||
main()
|
||||
10
remotenzb/state.py
Normal file
10
remotenzb/state.py
Normal file
@ -0,0 +1,10 @@
|
||||
import threading
|
||||
|
||||
state_lock = threading.Lock()
|
||||
|
||||
|
||||
state = {
|
||||
"current": None,
|
||||
"queue": [],
|
||||
"running": False,
|
||||
}
|
||||
30
remotenzb/transfer.py
Normal file
30
remotenzb/transfer.py
Normal file
@ -0,0 +1,30 @@
|
||||
import subprocess
|
||||
from state import state, state_lock
|
||||
from config import REMOTE
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def run_transfer(directory):
|
||||
logger.info(f"Running transfer for directory: {directory}")
|
||||
cmd = ["rsync", "-av", directory, REMOTE ]
|
||||
|
||||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
|
||||
with state_lock:
|
||||
state["current"] = directory
|
||||
state["running"] = True
|
||||
|
||||
|
||||
for line in process.stdout:
|
||||
logger.info("out" + line) # Print rsync output for debugging
|
||||
|
||||
|
||||
for line in process.stderr:
|
||||
logger.error("err" + line, end='') # Print rsync output for debugging
|
||||
|
||||
logger.info("Rsync process completed.")
|
||||
process.wait()
|
||||
|
||||
with state_lock:
|
||||
state["current"] = None
|
||||
state["running"] = False
|
||||
19
remotenzb/watcher.py
Normal file
19
remotenzb/watcher.py
Normal file
@ -0,0 +1,19 @@
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from state import state, state_lock
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
class Handler(FileSystemEventHandler):
|
||||
def on_created(self, event):
|
||||
if event.is_directory:
|
||||
with state_lock:
|
||||
logger.info(f"Directory created: {event.src_path}")
|
||||
state["queue"].append(event.src_path)
|
||||
|
||||
|
||||
def start_watcher(path):
|
||||
event_handler = Handler()
|
||||
observer = Observer()
|
||||
observer.schedule(event_handler, path, recursive=False)
|
||||
observer.start()
|
||||
return observer
|
||||
Loading…
x
Reference in New Issue
Block a user