✨ New Features: - Railway Worker for 24/7 automation (Node.js + BullMQ + Redis) - Connection monitoring service - Automation constants for centralized config - History logging service with auto cleanup - Mosvet_8 pengaduk support in kontrol page and Firebase 🐛 Bug Fixes: - Fixed StreamSubscription memory leak in dashboard_page - Added AppLifecycle observer for proper service management - Fixed race conditions with Redis queue - Improved error handling and user feedback - Better connection status monitoring 📚 Documentation: - DEPLOYMENT_GUIDE.md - Complete Railway deployment guide - BUGS_AND_FIXES.md - Bug report and fixes summary - RAILWAY_QUICK_START.md - Quick reference guide - IMPLEMENTATION_COMPLETE.md - Complete implementation summary - FULL_HISTORY_SYSTEM.md - History system documentation 🔧 Code Changes: - lib/main.dart - Added AppLifecycleObserver - lib/screens/dashboard_page.dart - Fixed memory leak - lib/screens/histori_page.dart - Full Firebase integration - lib/screens/kontrol_page.dart - Added pengaduk (mosvet_8) - lib/services/firebase_database_service.dart - Added mosvet_8, history methods - lib/services/kontrol_automation_service.dart - Use constants, connection check - lib/services/automation_constants.dart - NEW centralized constants - lib/services/connection_monitor_service.dart - NEW connection monitoring - lib/services/history_logging_service.dart - NEW auto logging service - railway-worker/ - NEW complete worker implementation Status: ✅ Production Ready |
||
|---|---|---|
| .. | ||
| .env.example | ||
| .gitignore | ||
| README.md | ||
| package.json | ||
| railway.json | ||
| worker.js | ||
README.md
ApsGo Railway Worker
Background worker service untuk sistem otomasi IoT ApsGo. Service ini berjalan 24/7 di cloud untuk menjalankan penjadwalan dan automation bahkan ketika aplikasi mobile ditutup atau handphone pengguna mati.
Features
- ✅ Waktu Mode: Penjadwalan berdasarkan waktu (cron-based)
- ✅ Sensor Mode: Otomasi berdasarkan threshold kelembapan tanah
- ✅ Auto History Logging: Record data sensor setiap 10 menit
- ✅ Redis Queue: Prevent race conditions dan manage concurrent tasks
- ✅ Graceful Shutdown: Clean shutdown dengan safety turn-off semua aktuator
- ✅ Health Monitoring: Auto health check setiap 5 menit
- ✅ Auto Cleanup: Hapus history lama otomatis (retain 30 hari)
Tech Stack
- Node.js: Runtime environment
- Firebase Admin SDK: Realtime Database integration
- BullMQ: Robust job queue dengan Redis
- Redis: In-memory database untuk queue dan caching
- Cron: Scheduled tasks
Setup Local Development
- Install dependencies:
npm install
- Copy
.env.exampleke.envdan isi dengan credentials Firebase Anda:
cp .env.example .env
- Setup Redis lokal (gunakan Docker):
docker run -d -p 6379:6379 redis:latest
- Run worker:
npm run dev # Development mode dengan nodemon
# atau
npm start # Production mode
Deploy to Railway
Lihat file DEPLOYMENT_GUIDE.md untuk step-by-step deployment ke Railway.
Environment Variables
| Variable | Description | Required |
|---|---|---|
FIREBASE_PROJECT_ID |
Firebase project ID | ✅ |
FIREBASE_CLIENT_EMAIL |
Firebase service account email | ✅ |
FIREBASE_PRIVATE_KEY |
Firebase service account private key | ✅ |
FIREBASE_DATABASE_URL |
Firebase Realtime Database URL | ✅ |
REDIS_HOST |
Redis hostname | ✅ |
REDIS_PORT |
Redis port (default: 6379) | ❌ |
REDIS_PASSWORD |
Redis password (if required) | ❌ |
Architecture
Flutter App (Mobile)
↕
Firebase Realtime DB ← ESP32/Hardware
↕
Railway Worker (This service)
↕
Redis Queue
How It Works
Waktu Mode
- Worker check Firebase
/kontrolsetiap 30 detik - Jika
waktu_1atauwaktu_2match dengan waktu sekarang, add job ke queue - Job akan diprocess oleh worker untuk nyalakan pompa dan valve
- Setelah durasi selesai, otomatis matikan
Sensor Mode
- Worker listen ke Firebase
/datasecara realtime - Jika
soil_X<batas_bawah, trigger watering untuk pot tersebut - Ada cooldown 2 menit per pot untuk prevent over-watering
- Support 2 mode:
fixed(durasi tetap) dansmart(sampai mencapai batas_atas)
Safety Features
- Concurrency: 1 (hanya 1 job diprocess pada satu waktu)
- Debouncing: Minimum 2 menit antar penyiraman per pot
- Error handling: Jika error, otomatis turn OFF semua aktuator
- Graceful shutdown: Clean up resources saat restart/shutdown
Monitoring
Worker akan log semua aktivitas ke console:
- ✅ Success operations
- ❌ Errors dengan details
- 💧 Watering jobs progress
- 📊 History logging
- 💚 Health check status
Di Railway dashboard, Anda bisa:
- View logs realtime
- Monitor CPU/Memory usage
- Setup alerts untuk failures
Maintenance
Manual Queue Management
Untuk clear queue (jika ada masalah):
const { Queue } = require('bullmq');
const Redis = require('ioredis');
const redis = new Redis(process.env.REDIS_URL);
const queue = new Queue('watering', { connection: redis });
// Clear all jobs
await queue.obliterate();
Database Cleanup
History otomatis di-cleanup setiap hari jam 2 pagi, hanya retain 30 hari terakhir.
Troubleshooting
Worker tidak berjalan
- Check environment variables
- Check Firebase credentials
- Check Redis connection
Job tidak diprocess
- Check queue status di logs
- Verify Firebase rules mengizinkan admin access
- Check concurrency setting
Memory leak
- Worker menggunakan BullMQ yang sudah optimize untuk long-running process
- Auto cleanup completed jobs (retain last 100)
- Auto cleanup failed jobs (retain last 50)
License
MIT