TKK_E32230814/railway-worker
Wizznu 206117d26a fix: Turn OFF pumps during break between pots to save power 2026-06-02 08:20:34 +07:00
..
.dockerignore feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
.env.example feat: Add Railway Worker, bug fixes, and mosvet_8 pengaduk 2026-02-10 07:22:43 +07:00
.env.template feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
.gitignore feat: Add Railway Worker, bug fixes, and mosvet_8 pengaduk 2026-02-10 07:22:43 +07:00
DEBUGGING_LOGS.md feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
Dockerfile feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
FLEXIBLE_SCHEDULE_GUIDE.md APSGO V2.1 NEW APS 2026-02-16 11:40:41 +07:00
README.md railway-worker: set auto-log 30m and cleanup retention 10 days 2026-03-24 23:05:20 +07:00
SETUP_FIREBASE.md feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
URGENT_FIX.md feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
WATERING_MODE_CHANGES.md feat: Change watering mode from parallel to sequential (bergiliran) with 30s break between pots 2026-06-02 07:50:05 +07:00
check-queue.js feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
debug.js feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
extract-credentials.js feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
firebase-example.json APSGO V2.1 NEW APS 2026-02-16 11:40:41 +07:00
migrate-history-source.js chore(railway): update worker and add history migration script 2026-03-26 18:27:01 +07:00
package.json feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
railway.json feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
setup-firebase-jadwal.js APSGO V2.1 NEW APS 2026-02-16 11:40:41 +07:00
setup-firebase-threshold.js feat: Threshold-based sensor system with unlimited threshold configs 2026-02-16 12:30:13 +07:00
test-firebase-connection.js feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
test-firebase-direct.js feat: Complete ApsGo system with Railway Worker automation 2026-02-11 17:17:22 +07:00
worker.js fix: Turn OFF pumps during break between pots to save power 2026-06-02 08:20:34 +07:00

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 30 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 10 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

  1. Install dependencies:
npm install
  1. Copy .env.example ke .env dan isi dengan credentials Firebase Anda:
cp .env.example .env
  1. Setup Redis lokal (gunakan Docker):
docker run -d -p 6379:6379 redis:latest
  1. 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 /kontrol setiap 30 detik
  • Jika waktu_1 atau waktu_2 match 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 /data secara 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) dan smart (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 10 hari terakhir.

Troubleshooting

Worker tidak berjalan

  1. Check environment variables
  2. Check Firebase credentials
  3. Check Redis connection

Job tidak diprocess

  1. Check queue status di logs
  2. Verify Firebase rules mengizinkan admin access
  3. 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