UNPKG

8.18 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || function (d, b) {
3 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4 function __() { this.constructor = d; }
5 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6};
7var nest_1 = require("./nest");
8var fileJob_1 = require("./../job/fileJob");
9var folderJob_1 = require("./../job/folderJob");
10var node_watch = require("node-watch"), fs = require("fs"), path_mod = require("path"), tmp = require("tmp"), mkdirp = require("mkdirp"), _ = require("lodash");
11/**
12 * A folder nest is a nest which contains a backing folder at a specific path. If the folder does not exist,
13 * antfarm can optionally create it.
14 */
15var FolderNest = (function (_super) {
16 __extends(FolderNest, _super);
17 function FolderNest(e, path, allowCreate) {
18 var nest_name = path_mod.basename(path);
19 _super.call(this, e, nest_name);
20 this.allowCreate = allowCreate;
21 this.checkDirectorySync(path);
22 this.path = path;
23 this.heldJobs = [];
24 }
25 /**
26 * Check if the path for the backing folder is created. If not, optionally create it.
27 * @param directory
28 */
29 FolderNest.prototype.checkDirectorySync = function (directory) {
30 var fn = this;
31 try {
32 fs.statSync(directory);
33 }
34 catch (e) {
35 if (fn.allowCreate) {
36 mkdirp.sync(directory);
37 fn.e.log(1, "Directory \"" + directory + "\" was created since it did not already exist.", this);
38 }
39 else {
40 fn.e.log(3, "Directory \"" + directory + "\" did not exist and was not created.", this);
41 }
42 }
43 };
44 /**
45 * Function that creates and arrives new jobs. Can produce file or folder jobs.
46 * @param path
47 * @param arrive
48 * @returns {FolderJob|FileJob}
49 */
50 FolderNest.prototype.createJob = function (path, arrive) {
51 if (arrive === void 0) { arrive = true; }
52 var fl = this;
53 var job;
54 // Verify file still exists, node-watch fires on any change, even delete
55 try {
56 fs.accessSync(path, fs.F_OK);
57 // Check job is folder
58 var path_stats = fs.lstatSync(path);
59 if (path_stats.isDirectory()) {
60 job = new folderJob_1.FolderJob(fl.e, path);
61 job.createFiles(function () {
62 if (arrive) {
63 // Trigger arrived
64 fl.arrive(job);
65 }
66 });
67 }
68 else if (path_stats.isFile()) {
69 job = new fileJob_1.FileJob(fl.e, path);
70 if (arrive) {
71 // Trigger arrived
72 fl.arrive(job);
73 }
74 }
75 else {
76 throw "Path is not a file or folder!";
77 }
78 }
79 catch (e) {
80 // It isn't accessible
81 fl.e.log(0, "Job creation ignored because file did not exist.", fl);
82 }
83 return job;
84 };
85 /**
86 * Initial load of the contents of the directory.
87 * @param hold {boolean} Optional flag to hold jobs found.
88 */
89 FolderNest.prototype.load = function (hold) {
90 if (hold === void 0) { hold = false; }
91 var fl = this;
92 fs.readdir(fl.path, function (err, items) {
93 if (items) {
94 items = items.filter(function (item) { return !(/(^|\/)\.[^\/\.]/g).test(item); });
95 items.forEach(function (filename) {
96 var filepath = fl.path + path_mod.sep + filename;
97 var job;
98 if (hold === false) {
99 fl.createJob(filepath, true); // Arrives as well
100 }
101 else {
102 job = fl.createJob(filepath, false);
103 fl.holdJob(job);
104 }
105 });
106 }
107 });
108 };
109 /**
110 * Watches the folder.
111 * @param hold {boolean} Optional flag to hold jobs found.
112 */
113 FolderNest.prototype.watch = function (hold) {
114 if (hold === void 0) { hold = false; }
115 var fl = this;
116 var watch_options = {
117 recursive: false
118 };
119 node_watch(fl.path, watch_options, function (filepath) {
120 var job;
121 if (hold === false) {
122 job = fl.createJob(filepath, true); // Arrives as well
123 }
124 else {
125 job = fl.createJob(filepath, false);
126 fl.holdJob(job);
127 }
128 });
129 };
130 /**
131 * Watches and holds jobs found.
132 */
133 FolderNest.prototype.watchHold = function () {
134 var fl = this;
135 fl.load(true);
136 fl.watch(true);
137 };
138 /**
139 * Arrive function that calls the super.
140 * @param job
141 */
142 FolderNest.prototype.arrive = function (job) {
143 _super.prototype.arrive.call(this, job);
144 };
145 /**
146 * Picks up a job from another nest.
147 * @param job
148 * @param callback Callback is given the job in its parameter.
149 */
150 FolderNest.prototype.take = function (job, callback) {
151 // the other nest that this is taking from should provide a temporary location or local path of the job
152 var new_path = this.path + "/" + job.getBasename();
153 fs.renameSync(job.getPath(), new_path);
154 job.setPath(new_path);
155 callback(job);
156 };
157 /**
158 * Loads jobs that have piled up in the nest if it was not watched.
159 * No longer used.
160 * @returns {Array} Array of jobs
161 */
162 FolderNest.prototype.getUnwatchedJobs = function () {
163 var fl = this;
164 var jobs = [];
165 var fileArray = fs.readdirSync(fl.path);
166 var items = fileArray.filter(function (item) { return !(/(^|\/)\.[^\/\.]/g).test(item); });
167 items.forEach(function (filename) {
168 var filepath = fl.path + path_mod.sep + filename;
169 var job = fl.createJob(filepath, false);
170 jobs.push(job);
171 // fl.holdJob(job);
172 });
173 return jobs;
174 };
175 /**
176 * Returns all held jobs.
177 * @returns {(FileJob|FolderJob)[]}
178 */
179 FolderNest.prototype.getHeldJobs = function () {
180 return this.heldJobs;
181 };
182 /**
183 * Adds job to array of held jobs.
184 * @param job
185 */
186 FolderNest.prototype.holdJob = function (job) {
187 this.heldJobs.push(job);
188 };
189 /**
190 * Get a held job with a job id. Removes it from the held job queue,
191 * so you should move it out of the folder after using this.
192 * @param jobId
193 * @returns {FileJob|FolderJob}
194 * #### Example
195 * ```js
196 * var tunnel = af.createTunnel("Checkpoint example");
197 * var webhook = af.createWebhookNest(["test", "example"], "get");
198 * var holding_folder = af.createAutoFolderNest(["test", "checkpoint"]);
199 *
200 * var im = webhook.getInterfaceManager();
201 *
202 * // Watch for jobs, hold, and provide to the interface.
203 * im.checkNest(holding_folder);
204 * tunnel.watch(webhook);
205 *
206 * tunnel.run(function(job, nest){
207 * // Get the job_id from the webhook request
208 * var job_id = job.getParameter("job_id");
209 * // Get the held job from the holding folder
210 * var checkpoint_job = holding_folder.getHeldJob(job_id);
211 * // Move somewhere else
212 * checkpoint_job.move(af.createAutoFolderNest(["test", "outfolder"]));
213 * });
214 * ```
215 */
216 FolderNest.prototype.getHeldJob = function (jobId) {
217 var f = this;
218 var job = _.find(f.getHeldJobs(), function (j) { return j.getId() === jobId; });
219 var jobIndex = _.findIndex(f.getHeldJobs(), function (j) { return j.getId() === jobId; });
220 if (!job) {
221 f.e.log(3, "Job ID " + jobId + " could not be found in the " + f.getHeldJobs().length + " pending held jobs.", f);
222 }
223 else {
224 f.heldJobs.splice(jobIndex, 1);
225 }
226 return job;
227 };
228 return FolderNest;
229}(nest_1.Nest));
230exports.FolderNest = FolderNest;