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

fake.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. let data = fs.readFileSync(filename, {encoding: "utf8"});
  13. let json = {jobs:[]};
  14. try {
  15. json = JSON.parse(data);
  16. } catch(e) {
  17. console.log('error', e);
  18. }
  19. return json;
  20. }
  21. function write(obj) {
  22. fs.writeFileSync(filename, JSON.stringify(obj));
  23. }
  24. // Read synchronously, make an update, rewrite
  25. function update(cb) {
  26. write(cb(load()));
  27. }
  28. module.exports = function() {
  29. function hset(key, field, value) {
  30. update(function(current){
  31. current[key] = current[key] || {};
  32. current[key][field] = value;
  33. return current;
  34. });
  35. }
  36. function hgetall(key, cb) {
  37. var current = load();
  38. if (!current) {
  39. return cb(null, null);
  40. }
  41. return cb(null, current[key] === undefined ? null : current[key]);
  42. }
  43. function hincr(key, field) {
  44. update(function(current){
  45. current[key] = current[key] || {};
  46. current[key][field] = current[key][field] || 0;
  47. current[key][field]++;
  48. return current;
  49. });
  50. }
  51. function getJobList(cb) {
  52. var current = load();
  53. return cb(null, current.jobs || []);
  54. }
  55. function addJob(settings) {
  56. update(function(current){
  57. current.jobs = current.jobs || [];
  58. current.jobs.push(settings);
  59. return current;
  60. });
  61. }
  62. function getJob(cb) {
  63. var job = null;
  64. update(function(current){
  65. if (current.jobs && current.jobs.length) {
  66. job = current.jobs.shift();
  67. }
  68. return current;
  69. });
  70. return cb(null, job);
  71. }
  72. function quit() { }
  73. function clean(cb) {
  74. fs.unlink(filename, cb);
  75. }
  76. // Random delay to minimize collision w/o Redis
  77. function delay() {
  78. return Math.random() * 1000;
  79. }
  80. return {
  81. setField: hset,
  82. getHash: hgetall,
  83. incrementField: hincr,
  84. getJobList: getJobList,
  85. addJob: addJob,
  86. getJob: getJob,
  87. quit: quit,
  88. cleanJobs: clean,
  89. workerDelay: delay
  90. };
  91. };