Skip to content

Instantly share code, notes, and snippets.

View UsamaAshraf's full-sized avatar

Usama Ashraf UsamaAshraf

View GitHub Profile
public function up()
{
Schema::table('jobs', function (Blueprint $table) {
$table->dropIndex('jobs_queue_reserved_reserved_at_index');
$table->dropColumn('reserved');
$table->index(['queue', 'reserved_at']);
});
Schema::table('failed_jobs', function (Blueprint $table) {
$table->longText('exception')->after('payload');
@UsamaAshraf
UsamaAshraf / Gemfile
Last active May 23, 2021 16:42
N+1 Queries, Batch Loading and Active Model Serializers
# ...
# https://github.com/exAspArk/batch-loader
gem 'batch-loader'
@UsamaAshraf
UsamaAshraf / backend-Dockerfile
Last active January 7, 2019 14:23
Scaling Out With Docker and Nginx
# Use a standard Node image from Docker Hub
FROM node:boron
# The Dockerfile's author
LABEL Usama Ashraf
# Create a directory in the container where the code will be placed
RUN mkdir -p /backend-dir-inside-container
# Set this as the default, working directory.
@UsamaAshraf
UsamaAshraf / docker-compose.yml
Last active April 27, 2018 10:00
Docker Compose file for Python & Go services communicating via RabbitMQ
# docker-compose.yml
version: "3.2"
services:
rabbitmq-server:
build: ./rabbitmq-server
python-service:
build: ./python-service
# 'rabbitmq-server' will be available as a network reference inside this service
# main.py
from flask import Flask
from flask import request
from flask import jsonify
from services.user_event_handler import emit_user_profile_update
app = Flask(__name__)
@app.route('/users/<int:user_id>', methods=['POST'])
@UsamaAshraf
UsamaAshraf / user_event_handler.py
Last active April 27, 2018 09:59
RabbitMQ publisher in Python
# services/user_event_handler.py
import pika
import json
def emit_user_profile_update(user_id, new_data):
# 'rabbitmq-server' is the network reference we have to the broker,
# thanks to Docker Compose.
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq-server'))
channel = connection.channel()
@UsamaAshraf
UsamaAshraf / main.go
Last active April 27, 2018 09:59
RabbitMQ subscriber in Go
// main.go
package main
import (
"fmt"
"log"
"github.com/streadway/amqp"
)
@UsamaAshraf
UsamaAshraf / service_worker_manager.rb
Last active May 5, 2018 15:41
A simple, custom Rails middleware class
# app/middleware/service_worker_manager.rb
class ServiceWorkerManager
# We’ll pass 'service_workers' when we register this middleware.
def initialize(app, service_workers)
@app = app
@service_workers = service_workers
end
def call(env)
@UsamaAshraf
UsamaAshraf / development.rb
Created May 4, 2018 04:20
Register a custom Rails middleware
# config/environments/development.rb
# …
# Add our own middleware before the ActionDispatch::Static
# middleware and pass it an array of service worker URIs as a
# parameter.
config.middleware.insert_before ActionDispatch::Static, ServiceWorkerManager, ['service-worker.js']
// service-worker.js
// Changing the cache version will cause existing cached resources to be
// deleted the next time the service worker is re-installed and re-activated.
const CACHE_VERSION = 1;
const CURRENT_CACHE = `your-app-name-cache-v-${CACHE_VERSION}`;
const OFFLINE_PAGE_URL = 'offline/offline.html';
const ASSETS_TO_BE_CACHED = ['offline/offline.css', 'offline/offline.jpg', OFFLINE_PAGE_URL];
self.addEventListener('install', event => {