-
Notifications
You must be signed in to change notification settings - Fork 0
Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js'
#4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Node.js dependencies | ||
| node_modules/ | ||
| package-lock.json | ||
| npm-debug.log* | ||
|
|
||
| # WhatsApp session data | ||
| .wwebjs_auth/ | ||
| .wwebjs_cache/ | ||
|
|
||
| # Python | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
| .Python | ||
| *.egg-info/ | ||
| dist/ | ||
| build/ | ||
|
|
||
| # App data | ||
| chat_history.db | ||
| chat_history_archive.db | ||
| uploads/ | ||
| Sandbox/ | ||
| identity.txt | ||
|
|
||
| # Environment | ||
| .env | ||
| .env.* | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3626,12 +3626,41 @@ def get_thumb_route(img_hash: str): | |||||||||||||||||||
| # YARDIMCI FONKSİYONLAR | ||||||||||||||||||||
| # ═══════════════════════════════════════════════════════════════ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def ensure_npm_deps() -> None: | ||||||||||||||||||||
| """node_modules eksikse npm install çalıştır.""" | ||||||||||||||||||||
| node_modules = APP_DIR / "node_modules" | ||||||||||||||||||||
| pkg_json = APP_DIR / "package.json" | ||||||||||||||||||||
| if not pkg_json.exists(): | ||||||||||||||||||||
| return | ||||||||||||||||||||
| if not node_modules.exists(): | ||||||||||||||||||||
| log.info("node_modules bulunamadı, 'npm install' çalıştırılıyor…") | ||||||||||||||||||||
| try: | ||||||||||||||||||||
| result = subprocess.run( | ||||||||||||||||||||
| ['npm', 'install'], | ||||||||||||||||||||
| cwd=str(APP_DIR), | ||||||||||||||||||||
| capture_output=True, | ||||||||||||||||||||
| text=True, | ||||||||||||||||||||
| timeout=300, | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| if result.returncode == 0: | ||||||||||||||||||||
| log.info("npm install başarılı.") | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| output = (result.stdout.strip() + "\n" + result.stderr.strip()).strip() | ||||||||||||||||||||
| log.error(f"npm install başarısız (kod {result.returncode}): {output}") | ||||||||||||||||||||
| except FileNotFoundError: | ||||||||||||||||||||
| log.error("npm bulunamadı. Node.js kurulu olduğundan emin olun.") | ||||||||||||||||||||
| except subprocess.TimeoutExpired: | ||||||||||||||||||||
| log.error("npm install zaman aşımına uğradı.") | ||||||||||||||||||||
|
Comment on lines
+3645
to
+3653
|
||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def ensure_bot_file() -> Path: | ||||||||||||||||||||
| """whatsapp_bot.js dosyasının var olduğunu doğrula.""" | ||||||||||||||||||||
| """whatsapp_bot.js dosyasının var olduğunu ve npm bağımlılıklarının kurulu olduğunu doğrula.""" | ||||||||||||||||||||
| bot_path = APP_DIR / "whatsapp_bot.js" | ||||||||||||||||||||
| if not bot_path.exists(): | ||||||||||||||||||||
| log.error(f"whatsapp_bot.js bulunamadı: {bot_path}") | ||||||||||||||||||||
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| ensure_npm_deps() | ||||||||||||||||||||
|
Comment on lines
3660
to
+3663
|
||||||||||||||||||||
| log.error(f"whatsapp_bot.js bulunamadı: {bot_path}") | |
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | |
| else: | |
| ensure_npm_deps() | |
| msg = f"whatsapp_bot.js bulunamadı: {bot_path}" | |
| log.error(msg) | |
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | |
| raise FileNotFoundError(msg) | |
| ensure_npm_deps() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure_npm_deps() can be reached from the threaded Flask server (/api/bot/start) and potentially by multiple gunicorn workers; as written, concurrent calls can launch multiple
npm installprocesses against the same directory, risking corrupted/partial installs. Consider adding a process-level/thread-level lock (and ideally an inter-process file lock) around the install path and re-checking node_modules after acquiring the lock.