Minimal unified shortcuts across editors and platforms for programming

※ The cmd for macOS is the ctrl for windows.

Redis as simple distribution lock for simple cases

I came across a system issue recently that some of our batch jobs written as ruby rake tasks ended up having duplicated invocations, which causes either duplicated records in database or duplicated alerts in slack. It is definitely an infra issue but until it is fixed from infra side, I need figure out a way to address it from the coding perspective. Trying to rewrite everything as idempotent as possible is not an option and neither is enhancing application level validations or database uniqueness constraints, because not all jobs can be idempotent and not all jobs write to databases, besides the issues are caused by race conditions not from two threads but from two processes. After some research and experiment, I decided to use Redis as a simple distribution lock solution, which is already available in our application and needs zero extra setup, and it is a single instance therefore no replication sync concern. Redis is single-threaded architecture so no matter how many concurrent clients try to write to it, it is guaranteed to be in sequential and atomic. I figure out I could make use of this nature to fix the issue faced.

Minimum commands to survive tmux

tmux is a terminal multiplexer. It is very useful when we need run long-running tasks on remote servers on the background and check the results later without the need to keep a constant live ssh connection and worrying about network failure causing the task to exit unexpectedly. Here is a list of minimum commands to meet most of the use cases.

How to write single file source code executable in java

Since JDK 11, Java supports executing source file directly without the need to first compile to class file, which makes it possible to write scripting in Java, as is usually done with dynamic programming languages like python, ruby, or nodejs. This post serves as an example as well as a quick reference on how to achieve it.

Convert epoch millisecond timestamp to readable datetime

In many cases such as when dealing with API response in JSON format, working on JavaScript assets embedded on websites, and so on, we keep seeing 13-digit long numbers very frequently and we probably know it is epoch timestamp originating from code like new Date().getTime(), however, as a human we just can not quickly tell what date and time it represents. There is a convenient website named Epoch Converter that can help convert it to human-readable format easily but most of the time for developers, there are even quicker ways than opening the website, which is by using the interactive shell that comes with many programming languages.

Upgrade to ruby 3.0.5

Ruby 3.0 came with a few breaking changes which would cause much hassle for a typical rails app that uses a lot of gems, especially the keyword argument behavior breaking change and the removal of the URI.escape method.

Import css with css module scripts in esm

Traditionally there are a few ways to statically add css to a page, and via JavaScript it can also be dynamically added by either creating a link element with rel="stylesheet" and href set to a css file location, or creating a style element with css definitions as its content. Now there is a 3rd way: importing css directly from within an ESM script tag, similarly to the way of importing JavaScript resources, via a feature called CSS Module scripts.

Nodejs finally has file.readLines() builtin

When dealing with large text files such as parsing log files, it is always preferred to load/iterate the content line by line instead of reading everything into memory for efficiency and performance reasons. In Nodejs, it has always been a hassle because the way of reading line by line looks hacky and too low-level from a daily user’s perspective. However, that’s not the case any more thanks to a PR merged into the latest version of v18.11.0, there is now FileHandle.prototype.readLines builtin which makes it very convenient to use.

Wait and retry in Nodejs

In other programming languages such as Java, Ruby and Python, there are Thread.sleep(), sleep, and time.sleep() methods to pause the execution of the current thread and wait after specified time to continue the execution, and when used together with loops, it can help address race condition issues for example. In node.js, and generally JavaScript, the way to pause and wait may not seem very obvious and frequently seen due to the nature of single-threaded, event-driven, and asyncronous architecture. However, since the use of wait and retry is still needed and helpful under certain circumstances, it is possible to simulate similar functions and effects in nodejs or JavaScript.

How to keep ssh connection alive

It becomes annoying sometimes if ssh connection gets terminated very soon if no messages sent between the ssh server and client for sometime but there are ways to keep it alive for long or forever from either client or server side.