Kaynağa Gözat

Initial commit

Gamaliel Espinoza 7 yıl önce
işleme
1a5f31ac7f
4 değiştirilmiş dosya ile 181 ekleme ve 0 silme
  1. 59 0
      .gitignore
  2. 3 0
      index.js
  3. 105 0
      ondemand.js
  4. 14 0
      package.json

+ 59 - 0
.gitignore

@@ -0,0 +1,59 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Bower dependency directory (https://bower.io/)
+bower_components
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules/
+jspm_packages/
+
+# Typescript v1 declaration files
+typings/
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# dotenv environment variables file
+.env
+

+ 3 - 0
index.js

@@ -0,0 +1,3 @@
+"use strict";
+require("./ondemand").start();
+console.log("Ondemand running...");

+ 105 - 0
ondemand.js

@@ -0,0 +1,105 @@
+const config = require("fourier-common").config;
+const db = require("fourier-common").fbconn.database();
+const spawn = require("child_process").spawn;
+const path = require("path");
+
+var working = false;
+
+var queue = {};
+
+var od_ref = db.ref("devices").child(config.device_id).child("ondemand");
+
+var proc;
+
+function process_queue() {
+  if (working) return;
+  working = true;
+
+  //var key = Object.keys(queue)[0];
+  //if (!key) return;
+
+  //var job = queue[key];
+
+  //delete queue[key];
+  var job = queue.shift();
+  if(!job) {
+    console.log("nothing left to do");
+    working = false;
+    return;
+  }
+
+  var key = job.key;
+
+  if (job.status == "done" || job.status === "no_index") {
+    setTimeout(() => {
+      working = false;
+      process_queue();
+    }, 1000);
+    return;
+  }
+
+  od_ref.child(key).update({status: "in_progress"});
+
+  var params = [
+    path.join(config.installDir,"fourier/process_timespan_nodb.py"),
+    config.device_id,
+    job.station,
+    job.fromTime,
+    job.toTime,
+    job.ad,
+    key,
+  ];
+
+  if (job.ads) params.push(job.ads.join(","));
+
+  console.log("ondemand: " + params.join(" "));
+
+  proc = spawn("python", params);
+  proc.stdout.on("data", data => {
+    console.log("ondemand data: " + data);
+  });
+  proc.stderr.on("data", data => {
+    console.log("ondemand error: " + data);
+  });
+  proc.on("exit", code => {
+    working = false;
+    proc = null;
+    if (code == 2) {
+      od_ref.child(key).update({status: "no_index"});
+    } else {
+      od_ref.child(key).update({status: "done"});
+    }
+    console.log("done", key);
+    process_queue();
+  });
+}
+
+function addAndProc(key, job) {
+    //queue[key] = job;
+  queue.push(job);
+  job.key = key;
+  process_queue();
+}
+
+function start() {
+  queue = [];
+
+  od_ref.on("child_added", snp => {
+    console.log("added", snp.key);
+    addAndProc(snp.key, snp.val());
+  });
+
+  //od_ref.on("child_changed", snp => {
+  //  addAndProc(snp.key, snp.val());
+  //});
+}
+
+function stop() {
+  od_ref.off();
+  queue = {};
+}
+
+module.exports = {
+  start: start,
+  stop: stop,
+};

+ 14 - 0
package.json

@@ -0,0 +1,14 @@
+{
+  "name": "fourier-ondemand",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "dependencies": {
+    "fourier-common": "file:../common/"
+  },
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC"
+}