decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 4.38 kB
JavaScript
module.exports=function(t){var e=require("fs"),n=require("bluebird"),r=require("underscore"),o=require("path"),u=require("node-targz"),c=require("@hapi/joi"),a=require("clusterpost-model"),i=require("clusterpost-lib");i.setClusterPostServer(t.uri),i.setAgentOptions({rejectUnauthorized:!1}),i.setUserToken(t.token);var s={getDataProvider:function(){return t.dataprovider.hostname},uploadDocumentDataProvider:function(t){return i.updateDocument(t)},getDocument:function(t){return c.assert(t,c.string().alphanum()),i.getDocument(t).then(function(t){return c.assert(t,a.job),t})},deleteDocument:function(t){return i.deleteJob(t._id)}};const d=function(t,n){e.readdirSync(t).forEach(function(r){try{var u=o.join(t,r),c=e.statSync(u);c&&c.isDirectory()?d(u,n):n.push(u)}catch(t){console.error(t)}})};s.addDocumentDirectoryAttachment=function(t,e,u){var c=[];return d(o.join(e,u),c),n.map(c,function(n){var r=n.substr(e.length+1);return s.addDocumentAttachment(t,r,n)},{concurrency:1}).then(function(t){var e=!0;return r.each(t,function(t){e=e&&t.ok}),{name:u,status:t,ok:e}})},s.addDocumentAttachment=function(t,e,n){return c.assert(t._id,c.string().alphanum()),c.assert(e,c.string()),i.uploadFile(t._id,n,e)},s.fileExists=function(t,n){try{return e.statSync(o.join(t,n.name)),!0}catch(t){return!1}},s.savePromise=function(t,e,u){var c=r.find(t.inputs,function(t){return t.name===u.name});return c&&(!t._attachments||t._attachments[u.name]||c.remote||c.local)?i.getDocumentAttachmentSave(t._id,u.name,o.join(e,u.name)):n.reject({status:!1,error:"Document is missing attachment"+u.name})},s.getAllDocumentInputs=function(t,e){var r=t.inputs,o=[],u=[];if(!r)return n.all([]);for(var c=0;c<r.length;c++)u.push(!1);if(t.jobstatus&&t.jobstatus.downloadstatus){var a=t.jobstatus.downloadstatus;for(c=0;c<a.length;c++)a[c].status&&(u[c]=a[c])}for(c=0;c<r.length;c++){var i=r[c];u[c]&&s.fileExists(e,i)?o.push(u[c]):o.push(s.savePromise(t,e,i).catch(function(t){return console.error(t),t}))}return n.all(o)},s.createDirectoryCWD=function(n){var r=o.join(t.storagedir,n._id);try{e.mkdirSync(r)}catch(t){if("EEXIST"!==t.code)throw t}return r},s.compressdirectory=function(r,c){return new n(function(n,a){var i;"cwd"===c?i=o.join(t.storagedir,r._id):"/"===(i=o.join(t.storagedir,r._id,c)).substr(-1)&&(i=i.substr(0,i.length-1));var s=i+".tar.gz";try{e.statSync(i)?u.compress({source:i,destination:s},function(){n(s)}):a({error:"Directory not found: "+i})}catch(t){a({error:t})}})},s.compressAndAddAttachment=function(t,e,n){return s.compressdirectory(t,n).then(function(e){var n=o.basename(e);return s.addDocumentAttachment(t,n,e)}).catch(function(t){return t})};const f=function(t){var e=t.doc,n=t.output,r=t.cwd,u=s.getDocument(e._id);if("file"===n.type)return u.then(function(t){var e;return e=n.path?o.join(r,n.path):o.join(r,n.name),s.addDocumentAttachment(t,n.name,e)});if("tar.gz"===n.type)return u.then(function(t){return s.compressAndAddAttachment(t,r,n.name)});if("directory"===n.type)return u.then(function(t){return s.addDocumentDirectoryAttachment(t,r,n.name)});throw"Upload handler for "+n.type+"not available"};s.setAllDocumentOutputs=function(e){for(var r=o.join(t.storagedir,e._id),u=e.outputs,c=[],a=0;a<u.length;a++)c.push({doc:e,output:u[a],cwd:r});return n.map(c,f,{concurrency:1})};const m=function(t){return t.ok?t:f(t).catch(function(t){return t})};return s.checkAllDocumentOutputs=function(e){for(var r=e.jobstatus.uploadstatus,u=e.outputs,c=o.join(t.storagedir,e._id),a=[],i=0;i<u.length;i++)r&&r[i]&&r[i].ok?a.push(r[i]):a.push({doc:e,output:u[i],cwd:c});return n.map(a,m,{concurrency:1})},s.deleteFolderRecursive=function(t){var n;try{n=e.statSync(t)}catch(t){n=void 0}return!!n&&(e.readdirSync(t).forEach(function(n){var r=o.join(t,n);e.statSync(r).isDirectory()?s.deleteFolderRecursive(r):e.unlinkSync(r)}),e.rmdirSync(t),!0)},s.getJobsStatus=function(e){return t.executionserver?i.getExecutionServerJobs(t.executionserver,e):n.reject("No executionserver in configuration.",JSON.stringify(t),"Set the codename for the executionserver as 'executionserver: servername'")},s.getJobsQueue=function(){return s.getJobsStatus("QUEUE")},s.getJobsRun=function(){return s.getJobsStatus("RUN")},s.getJobsUploading=function(){return s.getJobsStatus("UPLOADING")},s.getJobsKill=function(){return s.getJobsStatus("KILL")},s.getJobsDelete=function(){return i.getDeleteQueue()},s};