Turn audio into a shareable video. forked from nypublicradio/audiogram

fake.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var fs = require("fs"),
  2. mkdirp = require("mkdirp"),
  3. path = require("path"),
  4. filename = path.join(__dirname, "..", "..", "..", ".jobs");
  5. // Initialize if doesn't exist
  6. try {
  7. fs.statSync(filename);
  8. } catch(e) {
  9. fs.writeFileSync(filename, "{}");
  10. }
  11. function load() {
  12. return JSON.parse(fs.readFileSync(filename, {encoding: "utf8"}));
  13. }
  14. function write(obj) {
  15. fs.writeFileSync(filename, JSON.stringify(obj));
  16. }
  17. // Read synchronously, make an update, rewrite
  18. function update(cb) {
  19. write(cb(load()));
  20. }
  21. module.exports = function() {
  22. function hset(key, field, value) {
  23. update(function(current){
  24. current[key] = current[key] || {};
  25. current[key][field] = value;
  26. return current;
  27. });
  28. }
  29. function hgetall(key, cb) {
  30. var current = load();
  31. return cb(null, current[key] === undefined ? null : current[key]);
  32. }
  33. function hincr(key, field) {
  34. update(function(current){
  35. current[key] = current[key] || {};
  36. current[key][field] = current[key][field] || 0;
  37. current[key][field]++;
  38. return current;
  39. });
  40. }
  41. function getJobList(cb) {
  42. var current = load();
  43. return cb(null, current.jobs || []);
  44. }
  45. function addJob(settings) {
  46. update(function(current){
  47. current.jobs = current.jobs || [];
  48. current.jobs.push(settings);
  49. return current;
  50. });
  51. }
  52. function getJob(cb) {
  53. var job = null;
  54. update(function(current){
  55. if (current.jobs && current.jobs.length) {
  56. job = current.jobs.shift();
  57. }
  58. return current;
  59. });
  60. return cb(null, job);
  61. }
  62. function quit() { }
  63. function clean(cb) {
  64. fs.unlink(filename, cb);
  65. }
  66. // Random delay to minimize collision w/o Redis
  67. function delay() {
  68. return Math.random() * 1000;
  69. }
  70. return {
  71. setField: hset,
  72. getHash: hgetall,
  73. incrementField: hincr,
  74. getJobList: getJobList,
  75. addJob: addJob,
  76. getJob: getJob,
  77. quit: quit,
  78. cleanJobs: clean,
  79. workerDelay: delay
  80. };
  81. };