Skip to content

Instantly share code, notes, and snippets.

View StanisLove's full-sized avatar

Stanislav Kravchenko StanisLove

View GitHub Profile
### Nginx ###
check process nginx with pidfile /opt/nginx/logs/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 3 cycles then restart
if memory usage > 80% for 5 cycles then restart
if failed host 127.0.0.1 port 80 protocol http
then restart
if 3 restarts within 5 cycles then alert
@StanisLove
StanisLove / s3_folder_upload.rb
Created February 14, 2019 17:10 — forked from fleveque/s3_folder_upload.rb
Upload folder to S3 recursively with ruby, multi threads and aws-sdk v2 gem, based on http://avi.io/blog/2013/12/03/upload-folder-to-s3-recursively/
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
class S3FolderUpload
attr_reader :folder_path, :total_files, :s3_bucket, :include_folder
attr_accessor :files
# Initialize the upload class
@StanisLove
StanisLove / pg_remove_connections.sql
Created March 26, 2019 10:37
Postgres: remove all connections to DB
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'db_name'
AND pid <> pg_backend_pid();
threads = Array.new(8) do
Thread.new do
while arr.any?
mutex.synchronize do
end
elem = arr.pop
next if elem.nil?
end
@StanisLove
StanisLove / pg_flatten_json.sql
Created August 11, 2020 17:06 — forked from imamdigmi/pg_flatten_json.sql
Flattening json data in PostgreSQL
create or replace function create_jsonb_flat_view
(table_name text, regular_columns text, json_column text)
returns text language plpgsql as $$
declare
cols text;
begin
execute format ($ex$
select string_agg(format('%2$s->>%%1$L "%%1$s"', key), ', ')
from (
select distinct key
@StanisLove
StanisLove / class_controller.ex
Created August 12, 2020 19:31 — forked from elderbas/class_controller.ex
Elixir — Inserting Multiple Changesets Into Database - create batch
def create_batch(conn, %{"people" => people_params}) do
changesets = Enum.map(people_params, fn class ->
Person.changeset(%Person{}, person)
end)
result = changesets
|> Enum.with_index()
|> Enum.reduce(Ecto.Multi.new(), fn ({changeset, index}, multi) ->
Ecto.Multi.insert(multi, Integer.to_string(index), changeset)
end)

The Complete Guide to Nested Forms in Phoenix

I recently spent some time dealing with nested forms in Phoenix. Nested forms are great when you want to create multiple database records in a single transaction and associate them with each other. I am new to Phoenix and really struggled to find any resources that helped me with my specific problem. I decided to document what I learned in the process in hopes of helping others that are new to Elixir and Phoenix.

Here is my attempt at a one stop shop to learn everything you will need to know about nested forms. If you would like to view the GitHub repo you can check it out here.

Thanks to Heartbeat and Jose for excellent blog posts on nested forms. Also shoutout to Josh for showing me some examples at Ruby

@StanisLove
StanisLove / blocksToHtml.js
Created August 18, 2020 10:14 — forked from hagemann/blocksToHtml.js
Render EditorJS blocks to HTML.
const renderHtml = (blocks) => {
var html = blocks.map(block => {
switch (block.type) {
case 'code':
return `<pre><code>${ block.data.code }</pre></code>`
/**
* Original type is 'header' but I renamed the block to follow correct terminology:
* https://github.com/editor-js/header/issues/21
*/
import * as React from 'react';
import removeHTMLTags from './removeHTMLTags';
const safetySetInnerHTML = (text?: string) => {
if (!text) return null;
const allowedTags = ['div', 'p', 'li', 'ul', 'ol', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
.map((tag) => [`<${tag}>`, `</${tag}>`])
.flat();
@StanisLove
StanisLove / task_for_gen_server_use
Created August 30, 2020 08:59 — forked from Apelsinka223/task_for_gen_server_use.md
Задачка на использование GenServer
# Онлайн магазин
Необходимо создать API для онлайн магазина. Для покупки необходимо создать корзину, добавить туда товары и затем купить все товары из корзины. API подразумевает возможности просматривать список всех продуктов, список корзин.
Ожидается использование GenServer для хранения данных корзин.
## Методы API:
### 1. create_item