Skip to content

Instantly share code, notes, and snippets.

@spajak
spajak / np.py
Created March 19, 2018 19:34
Napiprojekt API. Pobieranie napisów do filmu
import sys
import hashlib
import base64
import os.path
import urllib.request, urllib.parse
import xml.etree.ElementTree as ET
URL='http://www.napiprojekt.pl/api/api-napiprojekt3.php'
@spajak
spajak / forking.php
Created June 11, 2019 16:35
Forking in PHP 7 done right. No zombies!!
<?php
/**
* Forking in PHP 7 done right. Requires PHP >= 7.1 and Process Control extension (pcntl)
*
* @author Sebastian Pająk
* Good article in the subject that inspired me: https://ruslanspivak.com/lsbaws-part3
*/
// Enable asynchronous signal handling (as of PHP 7.1)
@spajak
spajak / batch_cmd_arguments.cmd
Last active June 11, 2019 16:44
Batch script command line arguments variables
@echo off
rem Batch script (.bat or .cmd) command line arguments variables
rem %* - for all command line parameters (excluding the script name itself)
rem %0 - the command used to call the batch file
rem %1 - is the first command line parameter
rem %2 - is the second command line parameter, and so on till %9 (and SHIFT can be used for those after the 9th)
rem %~nx0 - the actual name of the batch file, regardless of calling method (some-batch.bat)
rem %~dp0 - drive and path to the script including trailing backslash (d:\scripts\)
@spajak
spajak / colors.sh
Last active June 11, 2019 16:46
Linux Bash terminal colors
#!/bin/bash
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#Background
for clbg in {40..47} {100..107} 49 ; do
@spajak
spajak / gulpfile.js
Last active June 11, 2019 16:48
Sample config for gulp
var gulp = require('gulp');
// Include Our Plugins
var jshint = require('gulp-jshint');
var sasslint = require('gulp-sass-lint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
@spajak
spajak / fib.py
Created June 11, 2019 17:07
Fibonacci sequence iterator in Python
class Fib:
"""Iterator that yields numbers in the Fibonacci sequence"""
def __init__(self, max):
self.max = max
def __iter__(self):
self.a = 0
self.b = 1
return self
@spajak
spajak / entware-ng.sh
Last active June 19, 2019 10:12
Bootstrap Entware-ng on Synology DS
#!/bin/bash
# Script installs Entware-ng (https://github.com/Entware/Entware-ng)
# on Synology DSM 6.2 under /opt directory.
# Must be run with sudo. Can be used as a scheduled task on system boot.
printf -- "---------------------------\n"
printf -- "| Installing Entware-ng.. |\n"
printf -- "---------------------------\n"
@spajak
spajak / Task.php
Last active June 28, 2019 17:06
PHP long running task with MySQL transaction + progress + state
<?php
/**
* Long running task with single MySQL transaction. Only one task with the same name
* can be running at a time. Next executed task waits untill previous finishes, or
* doesn't wait, but throws DomainException instead.
*
* Use it like so:
* $task = new Task('foo', $db);
* $task->run();
@spajak
spajak / php-error-logging.php
Last active June 30, 2019 12:55
PHP 7 runtime errors/exceptions handling done right
<?php
/**
* Simple, custom, bulletproof PHP 7 runtime errors/exceptions handling done right (I hope so :).
* By handling I mean logging to systemd journal or stderr, and die if any exception,
* error, warning, and even notice or strict occurs. This is not all-in-one
* or an ultimate solusion, but a good base to start with.
* Logs go to stderr or systemd journal (recommended, but optional). Systemd extension
* for PHP can be found at https://github.com/systemd/php-systemd
*/
@spajak
spajak / systemd-user.sh
Last active October 6, 2019 16:55
My steps to enable systemd manager for any user (In this case www-data)
# Create user runtime directory
sudo install -d -o www-data -g www-data -m 0700 /run/user/`id -u www-data`
# Start user manager
sudo systemctl start user@`id -u www-data`
# Optionally enable lingering (User manager is spawned for the user at boot and kept around after logouts).
sudo loginctl enable-linger www-data
# Test it