Skip to content

Instantly share code, notes, and snippets.

@JunilJacob
JunilJacob / good_job_asset_vite.rb
Created November 9, 2024 20:20
Copy GoodJob assets to public directory when using rails + vite + nginx
namespace :good_job do
desc "Copy GoodJob frontend assets to the public directory"
task copy_assets: :environment do
require "fileutils"
# GoodJob assets are stored inside the gem, so we copy them to the public directory
# so that they can be served by the Rails app.
good_job_version = GoodJob::VERSION.tr(".", "-")

In my application every time I send a newsletter to a User, I create a NewsletterDelivery record. I frequently want to be able to query, for each user, what is the most recent newsletter delivery record. This is called a "last n per group" query and a LATERAL JOIN is the best way to do it imo. But the query I've been using (and I've told people to use, and seen blogged about) is not very good, because the conditions don't get pushed down into the subselect which means that the query ends-up lateral-joining all the records before it applies the conditions for the association.

Instead of doing subselect_table.* the better query does association.id AS assocation_id, subselect_table.id, subselect_table.title, .... and enumerates over all of the columns. This allows the association query, which Active Record tacks on at the end as WHERE association_id = $1 or WHERE association_id IN ($1, $2, $3, ...) to be pushed down c

@JunilJacob
JunilJacob / bash_strict_mode.md
Created January 23, 2024 07:11 — forked from mohanpedala/bash_strict_mode.md
set -e, -u, -o, -x pipefail explanation

##Working configuration to accomplish 0-downtime deploy with unicorn 5.x and systemd on centos 7

The scope is to accomplish a 0-downtime reload of a unicorn service managed by Systemd on a Centos 7 distro.

The examples and assumptions that i found on the bogomips's unicorn repo seems not working for centos 7.

Below a working configuration tested on Centos 7 and unicorn 5.1

Any advice/remark will be appreciated

@JunilJacob
JunilJacob / with_retries.rb
Created June 27, 2022 18:45 — forked from cainlevy/with_retries.rb
with_retries{} helper to easily catch and retry exceptions in Ruby
module Retriable
# This will catch any exception and retry twice (three tries total):
# with_retries { ... }
#
# This will catch any exception and retry four times (five tries total):
# with_retries(:limit => 5) { ... }
#
# This will catch a specific exception and retry once (two tries total):
# with_retries(Some::Error, :limit => 2) { ... }
#
# unicorn_rails -c /data/github/current/config/unicorn.rb -E production -D
rails_env = ENV['RAILS_ENV'] || 'production'
# 16 workers and 1 master
worker_processes (rails_env == 'production' ? 16 : 4)
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
@JunilJacob
JunilJacob / CommonUtils.java
Created September 30, 2021 08:35
Upload Files in Selenium for different OS
package utility;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.WebElement;
public class CommonUtils {
@JunilJacob
JunilJacob / CryptoKeyGenerator.java
Created September 30, 2021 08:30
Encrypted Credentials Java Selenium
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class CryptoKeyGenerator {
public static void main(String[] args) throws NoSuchAlgorithmException {
SecretKey key = KeyGenerator.getInstance("AES").generateKey();
String encodedKey = Base64
@JunilJacob
JunilJacob / base_controller.rb
Last active October 23, 2019 08:16
Request Rate Limiter
class Api::V3::BaseController < ActionController::Base
include Limiter
before_action -> { rate_limit(key: "api_#{doorkeeper_token.application_id}", rate: 60, interval: 60) }
private
def handle_rate_limit_exception
log_exception RateLimitError.new("Rate limit reached for #{doorkeeper_token.application.name}")
render json: { error: { type: "rate_limit_exceeded", message: "Rate limit exceeded." } }, status: 429
@JunilJacob
JunilJacob / totp_2fa.rb
Created May 31, 2019 09:54 — forked from Nitrino/totp_2fa.rb
Rails mixin module for Two Factor Authentication (TOTP)
module OneTimePassword
# Concern containing logic and methods for OTP authentication.
# Is used Time-based One-time Password Algorithm(TOTP)
# https://tools.ietf.org/html/rfc6238
extend ActiveSupport::Concern
OTP_DIGITS = 6
OTP_NUMBER_OF_BACKUP_CODES = 10
OTP_BACKUP_CODE_LENGTH = 12