Skip to content

Commit

Permalink
Start on some tinkering with NATS and then give up in favour of just …
Browse files Browse the repository at this point in the history
…a claim system via Postgres table locking
  • Loading branch information
initialed85 committed Aug 27, 2024
1 parent f91cb04 commit b3e1301
Show file tree
Hide file tree
Showing 29 changed files with 3,069 additions and 232 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ DJANGOLANG_API_ROOT=/api DESTINATION_PATH=media ENABLE_PASSTHROUGH=1 CAMERA_NAME

# shell 8
DJANGOLANG_API_ROOT=/api DESTINATION_PATH=media ENABLE_PASSTHROUGH=1 CAMERA_NAME='Side gate' NET_CAM_URL=rtsp://192.168.137.33:554/Streaming/Channels/101 POSTGRES_DB=camry POSTGRES_PASSWORD=NoNVR\!11 go run ./cmd segment_producer

# shell 9

```

### Scratch
Expand Down
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ if ! command -v npm >/dev/null 2>&1; then
exit 1
fi

# we don't need natscli for tooling, but it's a handy debug tool
if ! command -v nats >/dev/null 2>&1; then
go install github.com/nats-io/natscli/nats@latest
fi

# ensure we've got a djangolang executable available (required for templating)
if [[ "${FORCE_UPDATE_DJANGOLANG}" == "1" ]] || ! command -v djangolang >/dev/null 2>&1; then
GOPRIVATE="${GOPRIVATE:-}" go install github.com/initialed85/djangolang@latest
Expand Down
8 changes: 7 additions & 1 deletion database/migrations/00001_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ CREATE TABLE
deleted_at timestamptz NULL DEFAULT NULL,
name text NOT NULL CHECK (trim(name) != ''),
stream_url text NOT NULL CHECK (trim(stream_url) != ''),
last_seen timestamptz NULL DEFAULT NULL
last_seen timestamptz NOT NULL DEFAULT to_timestamp(0),
-- claimed_at timestamptz NOT NULL DEFAULT to_timestamp(0) CHECK (claimed_at <= now()),
claimed_at timestamptz NOT NULL DEFAULT to_timestamp(0),
-- claim_duration interval NOT NULL DEFAULT interval '1 minute' CHECK (claim_duration > interval '0 seconds'),
claim_duration interval NOT NULL DEFAULT interval '1 minute',
-- claim_expires_at timestamptz NOT NULL DEFAULT to_timestamp(1) CHECK (claim_expires_at > claimed_at)
claim_expires_at timestamptz NOT NULL DEFAULT to_timestamp(1)
);

ALTER TABLE public.camera OWNER TO postgres;
Expand Down
17 changes: 14 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
volumes:
postgres:
media:

services:
Expand All @@ -12,6 +13,8 @@ services:
timeout: 4s
start_period: 10s
test: ["CMD", "pg_isready", "-h", "localhost", "-U", "postgres"]
volumes:
- postgres:/home/postgres/pgdata/data
environment:
- "POSTGRES_DB=camry"
- "POSTGRES_PASSWORD=NoNVR!11"
Expand All @@ -23,7 +26,7 @@ services:
postgres:
condition: service_healthy
image: migrate/migrate:v4.17.1
restart: on-failure
restart: no
stop_grace_period: 0s
volumes:
- ./database/migrations:/migrations
Expand All @@ -38,7 +41,7 @@ services:
condition: service_completed_successfully
# this obscure image is chosen because it has x86_64 and arm64 support
image: duvel/postgis:15-3.3
restart: on-failure
restart: no
stop_grace_period: 0s
entrypoint: ["/bin/bash", "-c"]
command: "PAGER=cat PGPASSWORD=NoNVR!11 psql -h postgres -p 5432 -U postgres -a camry -c 'VACUUM FULL; VACUUM ANALYZE;'"
Expand All @@ -58,7 +61,15 @@ services:
- "BASE_URL=/"
- "SWAGGER_JSON_URL=http://host.docker.internal:7070/api/openapi.json"
ports:
- "7071:8080"
- "7071:8080/tcp"

nats:
restart: unless-stopped
stop_grace_period: 0s
image: nats:2.10.18
ports:
- "4222:4222/tcp"
command: ["-js"]

api:
profiles:
Expand Down
10 changes: 3 additions & 7 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister";
import {
persistQueryClient,
removeOldestQuery,
} from "@tanstack/react-query-persist-client";
import { persistQueryClient, removeOldestQuery } from "@tanstack/react-query-persist-client";

import { default as createFetchClient } from "openapi-fetch";
import createClientForReactQuery from "openapi-react-query";
Expand Down Expand Up @@ -34,9 +31,8 @@ persistQueryClient({
persister: localStoragePersister,
});

const clientForReactQuery = createFetchClient<paths>({
export const clientForReactQuery = createFetchClient<paths>({
baseUrl: "/",
});

export const { useQuery, useMutation, useSuspenseQuery } =
createClientForReactQuery(clientForReactQuery);
export const { useQuery, useMutation, useSuspenseQuery } = createClientForReactQuery(clientForReactQuery);
152 changes: 151 additions & 1 deletion frontend/src/api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,20 @@ export type webhooks = Record<string, never>;
export interface components {
schemas: {
Camera: {
/** Format: int64 */
claim_duration?: number;
/** Format: date-time */
claim_expires_at?: string;
/** Format: date-time */
claimed_at?: string;
/** Format: date-time */
created_at?: string;
/** Format: date-time */
deleted_at?: string | null;
/** Format: uuid */
id?: string;
/** Format: date-time */
last_seen?: string | null;
last_seen?: string;
name?: string;
referenced_by_detection_camera_id_objects?: components["schemas"]["NullableArrayOfDetection"];
referenced_by_video_camera_id_objects?: components["schemas"]["NullableArrayOfVideo"];
Expand Down Expand Up @@ -550,6 +556,150 @@ export interface operations {
last_seen__desc?: string;
/** @description SQL ORDER BY _ ASC operator, value is ignored (presence of key is sufficient) */
last_seen__asc?: string;
/** @description SQL = operator */
claimed_at__eq?: string;
/** @description SQL != operator */
claimed_at__ne?: string;
/** @description SQL > operator, may not work with all column types */
claimed_at__gt?: string;
/** @description SQL >= operator, may not work with all column types */
claimed_at__gte?: string;
/** @description SQL < operator, may not work with all column types */
claimed_at__lt?: string;
/** @description SQL <= operator, may not work with all column types */
claimed_at__lte?: string;
/** @description SQL IN operator, permits comma-separated values */
claimed_at__in?: string;
/** @description SQL NOT IN operator, permits comma-separated values */
claimed_at__nin?: string;
/** @description SQL NOT IN operator, permits comma-separated values */
claimed_at__notin?: string;
/** @description SQL IS NULL operator, value is ignored (presence of key is sufficient) */
claimed_at__isnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claimed_at__nisnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claimed_at__isnotnull?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__l?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__like?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__nl?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__nlike?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__notlike?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__il?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__ilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__nil?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__nilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claimed_at__notilike?: string;
/** @description SQL ORDER BY _ DESC operator, value is ignored (presence of key is sufficient) */
claimed_at__desc?: string;
/** @description SQL ORDER BY _ ASC operator, value is ignored (presence of key is sufficient) */
claimed_at__asc?: string;
/** @description SQL = operator */
claim_duration__eq?: number;
/** @description SQL != operator */
claim_duration__ne?: number;
/** @description SQL > operator, may not work with all column types */
claim_duration__gt?: number;
/** @description SQL >= operator, may not work with all column types */
claim_duration__gte?: number;
/** @description SQL < operator, may not work with all column types */
claim_duration__lt?: number;
/** @description SQL <= operator, may not work with all column types */
claim_duration__lte?: number;
/** @description SQL IN operator, permits comma-separated values */
claim_duration__in?: number;
/** @description SQL NOT IN operator, permits comma-separated values */
claim_duration__nin?: number;
/** @description SQL NOT IN operator, permits comma-separated values */
claim_duration__notin?: number;
/** @description SQL IS NULL operator, value is ignored (presence of key is sufficient) */
claim_duration__isnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claim_duration__nisnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claim_duration__isnotnull?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__l?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__like?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__nl?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__nlike?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__notlike?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__il?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__ilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__nil?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__nilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_duration__notilike?: string;
/** @description SQL ORDER BY _ DESC operator, value is ignored (presence of key is sufficient) */
claim_duration__desc?: string;
/** @description SQL ORDER BY _ ASC operator, value is ignored (presence of key is sufficient) */
claim_duration__asc?: string;
/** @description SQL = operator */
claim_expires_at__eq?: string;
/** @description SQL != operator */
claim_expires_at__ne?: string;
/** @description SQL > operator, may not work with all column types */
claim_expires_at__gt?: string;
/** @description SQL >= operator, may not work with all column types */
claim_expires_at__gte?: string;
/** @description SQL < operator, may not work with all column types */
claim_expires_at__lt?: string;
/** @description SQL <= operator, may not work with all column types */
claim_expires_at__lte?: string;
/** @description SQL IN operator, permits comma-separated values */
claim_expires_at__in?: string;
/** @description SQL NOT IN operator, permits comma-separated values */
claim_expires_at__nin?: string;
/** @description SQL NOT IN operator, permits comma-separated values */
claim_expires_at__notin?: string;
/** @description SQL IS NULL operator, value is ignored (presence of key is sufficient) */
claim_expires_at__isnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claim_expires_at__nisnull?: string;
/** @description SQL IS NOT NULL operator, value is ignored (presence of key is sufficient) */
claim_expires_at__isnotnull?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__l?: string;
/** @description SQL LIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__like?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__nl?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__nlike?: string;
/** @description SQL NOT LIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__notlike?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__il?: string;
/** @description SQL ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__ilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__nil?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__nilike?: string;
/** @description SQL NOT ILIKE operator, value is implicitly prefixed and suffixed with % */
claim_expires_at__notilike?: string;
/** @description SQL ORDER BY _ DESC operator, value is ignored (presence of key is sufficient) */
claim_expires_at__desc?: string;
/** @description SQL ORDER BY _ ASC operator, value is ignored (presence of key is sufficient) */
claim_expires_at__asc?: string;
};
header?: never;
path?: never;
Expand Down
Loading

0 comments on commit b3e1301

Please sign in to comment.