Skip to content

Instantly share code, notes, and snippets.

View garenyondem's full-sized avatar
:shipit:

Garen Yöndem garenyondem

:shipit:
  • Afiniti
  • 07:00 (UTC +03:00)
View GitHub Profile
@garenyondem
garenyondem / cancellation-token.ts
Created December 21, 2023 09:30
C# style CancellationToken implementation in Typescript
export class CancellationToken {
#isCancellationRequested: boolean = false;
get isCancellationRequested() {
return this.#isCancellationRequested;
}
cancel() {
this.#isCancellationRequested = true;
}
}
@garenyondem
garenyondem / sqlite_backup.sh
Created May 15, 2022 15:09
Cron-based sqlite backup
#!/bin/bash -x
# Ensure script stops when commands fail.
set -e
# Backup & compress our database to the temp directory.
sqlite3 /path/to/db '.backup /tmp/db'
gzip /tmp/db
# Upload backup to S3 using a rolling daily naming scheme.
@garenyondem
garenyondem / restart_explorer.bat
Created January 19, 2022 12:32
fix unresponsive windows explorer
taskkill /im explorer.exe /f
start explorer.exe
exit
@garenyondem
garenyondem / gist:62a0c89c66a8fd6d80ff6cea504a1eab
Created December 28, 2021 19:49
Typescript Flatten two types
type Flatten<T> = T extends object ? {
[P in keyof T]: Flatten<T[P]>
} : T;
type A = { foo: boolean } & { bar: string };
type B = Flatten<A>; // { foo: boolean; bar: string; }
@garenyondem
garenyondem / promiseRetry.js
Last active December 23, 2021 20:23
Recursively retry promise function by given number of times and arguments
module.exports = function (func, times, ...args) {
return new Promise((resolve, reject) => {
if (times <= 0) {
return reject(new Error('Invalid input times'));
}
func.apply(this, args).then((result) => {
if (!result) {
throw new Error('Func returned no results');
}
return resolve(result);
@garenyondem
garenyondem / portainer-template.json
Last active September 23, 2021 14:39
Portainer Templates
{
"version": "2",
"templates": [
{
"type": 1,
"title": "Registry",
"description": "Docker image registry",
"categories": [
"docker"
],
@garenyondem
garenyondem / async-handlers.js
Created June 15, 2020 18:52
Async handler, wrapers, error handlers
exports.errorHandler = (response, err) => {
const statusCode = err.status || 500;
response.status(statusCode).json({
success: false,
error: err.message
});
};
exports.successHandler = (response, data) => {
response.json({
@garenyondem
garenyondem / uuid.ts
Created February 25, 2020 18:30
Create uuid
export function uuid() {
let rnd: number, val: { toString: (arg0: number) => string };
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
rnd = (Math.random() * 16) | 0;
val = c === "x" ? rnd : (rnd & 0x3) | 0x8;
return val.toString(16);
});
}
@garenyondem
garenyondem / RSA.js
Created January 25, 2020 13:11 — forked from fb55/index.js
RSA.js
(function(global){
var MathUtils = {
powermod: function powermod(num, exp, mod){
if(exp === 1) return num % mod;
if(exp & 1 === 1){ //odd
return (num * powermod(num, exp-1, mod)) % mod;
}
return Math.pow(powermod(num, exp/2, mod), 2) % mod;
},
#!/bin/bash
# Get ids of processes running longer than 6 hrs (21600 secs)
PIDS=$(ps eaxo etimes,bsdtime,pid,comm,cmd | grep node | grep command-line-processes | awk '{if ($1 >= 21600) print $3}')
for i in ${PIDS};
do {
PROC_FILE_PATH=$(ps eaxo pid,cmd | grep node | grep "$i"| awk '{print $3}');
SCRIPT_NAME=$(basename "$PROC_FILE_PATH");
printf "Killing $SCRIPT_NAME\n";