Skip to content

Commit db06567

Browse files
committed
added crash detection and some analysis stuff
1 parent d9da9ef commit db06567

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

service/src/main.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
* */
1818
import "dotenv/config";
19+
import fs from "fs";
1920
import http from "http";
2021

2122
import { AbsURL, OpenAPI } from "acts-util-core";
2223
import { Factory, GlobalInjector, HTTP } from "acts-util-node";
2324
import { APIRegistry } from "acts-util-apilib";
2425
import { DBConnectionsManager } from "./data-access/DBConnectionsManager";
25-
import { CONFIG_OIDP_ENDPOINT, CONFIG_ORIGIN, CONFIG_PORT, CONFIG_UPLOADDIR } from "./env";
26+
import { CONFIG_OIDP_ENDPOINT, CONFIG_ORIGIN, CONFIG_PORT, CONFIG_ROOTDIR, CONFIG_UPLOADDIR } from "./env";
2627
import { FtpSrv, GeneralError } from "ftp-srv";
2728
import { FTPFileSystem } from "./FTPFileSystem";
2829
import { StorageBackendsManager } from "./services/StorageBackendsManager";
@@ -35,6 +36,8 @@ import { AccessCounterService } from "./services/AccessCounterService";
3536
import { StorageBlocksManager } from "./services/StorageBlocksManager";
3637
import { FileUploadService } from "./services/FileUploadService";
3738

39+
const crashDetectionPath = CONFIG_ROOTDIR + "/crash_check";
40+
3841
async function DownloadPublicKey()
3942
{
4043
const sender = new HTTP.RequestSender();
@@ -129,6 +132,7 @@ async function BootstrapServer()
129132
console.log("Shutting server down...");
130133
GlobalInjector.Resolve(DBConnectionsManager).Close();
131134
GlobalInjector.Resolve(MessagingService).Close();
135+
fs.unlinkSync(crashDetectionPath);
132136
server.close();
133137
});
134138
}
@@ -160,5 +164,24 @@ function BootstrapFTPServer()
160164
});
161165
}
162166

163-
BootstrapServer();
164-
BootstrapFTPServer();
167+
function BootstrapService()
168+
{
169+
if(fs.existsSync(crashDetectionPath))
170+
{
171+
console.log("Service did crash :S");
172+
process.exit(1);
173+
return;
174+
}
175+
process.on("uncaughtException", (error, origin) => {
176+
console.log("Unhandled exception: ", error, origin);
177+
});
178+
process.on("unhandledRejection", (reason, promise) => {
179+
console.log("Unhandled rejection: ", reason, promise);
180+
});
181+
fs.writeFileSync(crashDetectionPath, "");
182+
183+
BootstrapServer();
184+
BootstrapFTPServer();
185+
}
186+
187+
BootstrapService();

service/src/services/FileUploadService.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export class FileUploadService
126126
blockIds.push(blockId);
127127
}
128128

129-
return await this.FindOrCreateBlob(hasher.digest("hex"), blockIds)
129+
console.log(blockIds);
130+
return await this.FindOrCreateBlob(hasher.digest("hex"), blockIds);
130131
}
131132

132133
private ProcessStreamInParallel(stream: Readable)
@@ -190,14 +191,17 @@ export class FileUploadService
190191
private async ProcessBlobBlock(blobBlock: Buffer)
191192
{
192193
const sha256sum = crypto.createHash("sha256").update(blobBlock).digest("hex");
193-
return this.ProcessBlobBlockHashed(blobBlock, sha256sum);
194+
return await this.ProcessBlobBlockHashed(blobBlock, sha256sum);
194195
}
195196

196197
private async ProcessBlobBlockHashed(blobBlock: Buffer, sha256sum: string): Promise<number>
197198
{
198199
const blockId = await this.blobsController.FindBlobBlock(blobBlock.byteLength, sha256sum);
199200
if(blockId !== undefined)
201+
{
202+
console.log("FOUND", blockId);
200203
return blockId;
204+
}
201205

202206
let newBlockId;
203207
try
@@ -208,6 +212,7 @@ export class FileUploadService
208212
{
209213
if(e?.code === "ER_DUP_ENTRY")
210214
return await this.ProcessBlobBlockHashed(blobBlock, sha256sum);
215+
console.log("error", e);
211216
throw e;
212217
}
213218
const storageBlock = await this.storageBackendsManager.StoreBlobBlock(blobBlock);

0 commit comments

Comments
 (0)