Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PeterGriffitz
Created December 24, 2020 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterGriffitz/172aa57cbdb40677cd6839bbc6c42236 to your computer and use it in GitHub Desktop.
Save PeterGriffitz/172aa57cbdb40677cd6839bbc6c42236 to your computer and use it in GitHub Desktop.
Floitet-Patroni-Zookeeper-Part2
FROM alpine:3.10
RUN apk add --no-cache curl jq bash
CMD ["/bin/sh"]
version: "3.7"
networks:
patroni_patroni:
external: true
services:
haproxy:
image: haproxy-patroni
networks:
- patroni_patroni
ports:
- 5000:5000
- 7000:7000
hostname: haproxy
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.hostname == floitet]
FROM haproxy:1.7
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
RUN mkdir /run/haproxy &&\
apt-get update -y &&\
apt-get install -y hatop &&\
apt-get clean
global
maxconn 100
stats socket /run/haproxy/haproxy.sock
stats timeout 2m # Wait up to 2 minutes for input
defaults
log global
mode tcp
retries 2
timeout client 30m
timeout connect 4s
timeout server 30m
timeout check 5s
listen stats
mode http
bind *:7000
stats enable
stats uri /
listen postgres
bind *:5000
option httpchk
http-check expect status 200
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
server patroni1 patroni1:5432 maxconn 100 check port 8091
server patroni2 patroni2:5432 maxconn 100 check port 8091
server patroni3 patroni3:5432 maxconn 100 check port 8091
#r "patroni-test/bin/Debug/net5.0/Npgsql.dll"
open System
open Npgsql
open System.Threading
open System.Diagnostics
let connStrignEntryPoint = "Host=haproxy;Port=5000;Username=approle;Password=appass;Database=postgres"
let dbName = "patronitestdb"
// time you want the scipt to be up writing to database
let testTimeLenght = TimeSpan.FromMinutes(5.)
let CreateDbIfNotPresent (conStr: string, dbName: string) =
use conn = new NpgsqlConnection(connStrignEntryPoint)
use command = new NpgsqlCommand(sprintf @"SELECT DATNAME FROM pg_catalog.pg_database WHERE DATNAME = '%s'" dbName, conn)
conn.Open()
let createDb () =
use cmd = new NpgsqlCommand( sprintf "CREATE DATABASE %s" dbName, conn)
cmd.ExecuteNonQuery() |> ignore
try
command.ExecuteScalar().ToString() |> ignore
conn.Close()
with
| :? System.NullReferenceException as ex -> createDb(); conn.Close()
CreateDbIfNotPresent(connStrignEntryPoint, dbName)
let connStringFinal = connStrignEntryPoint.Replace("postgres", dbName)
let WriteToTableForSetTime (connStr: string) =
use conn = new NpgsqlConnection(connStr)
conn.Open()
use cmd = new NpgsqlCommand(@"create table if not exists records (time time not null)", conn)
cmd.ExecuteNonQuery() |> ignore
conn.Close()
let mutable flag = true
let stopwatch = Stopwatch()
stopwatch.Start()
let writeTime () =
try
conn.Open()
use cmd = new NpgsqlCommand( @"insert into records values (@p)", conn)
let time = DateTimeOffset.Now
printfn "%O" time
cmd.Parameters.AddWithValue("p", time) |> ignore
cmd.ExecuteNonQuery() |> ignore
conn.Close()
with _ -> printfn "Error"; conn.Close()
while (flag) do
Thread.Sleep(1000)
writeTime() |> ignore
if (stopwatch.Elapsed > testTimeLenght) then flag <- false
stopwatch.Stop()
WriteToTableForSetTime(connStringFinal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment