Skip to content

Commit

Permalink
initial code (#6)
Browse files Browse the repository at this point in the history
* initial code commit
  • Loading branch information
l3uddz authored Feb 14, 2021
1 parent 3f55336 commit ce3807b
Show file tree
Hide file tree
Showing 53 changed files with 3,694 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: l3uddz
134 changes: 134 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
name: Build

on:
push:
branches:
- '*'
tags:
- 'v*'
pull_request:
types:
- opened
- reopened
- edited

jobs:
build:
runs-on: ubuntu-latest
steps:
# dependencies
- name: dependencies
run: |
curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sudo sh -s -- -b /usr/local/bin
# checkout
- name: checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

# setup go
- name: go
uses: actions/setup-go@v1
with:
go-version: 1.15
- run: go version
- run: go env

# cache
- name: cache
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# vendor
- name: vendor
run: |
make vendor
# build
- name: build
if: startsWith(github.ref, 'refs/tags/') == false
run: |
make snapshot
# get tag name
- name: tag_name
if: startsWith(github.ref, 'refs/tags/')
uses: little-core-labs/[email protected]
with:
tagRegex: "v?(.+)"

# publish
- name: publish
if: startsWith(github.ref, 'refs/tags/')
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REF: ${{ github.ref }}
run: |
make publish
# artifacts
- name: artifact_linux
uses: actions/upload-artifact@v2-preview
with:
name: build_linux
path: dist/*linux*

- name: artifact_darwin
uses: actions/upload-artifact@v2-preview
with:
name: build_darwin
path: dist/*darwin*

- name: artifact_windows
uses: actions/upload-artifact@v2-preview
with:
name: build_windows
path: dist/*windows*

# docker build (latest & tag)
- name: docker - build latest
if: startsWith(github.ref, 'refs/tags/') == true
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: l3uddz/nabarr/nabarr
dockerfile: docker/Dockerfile
tags: latest
tag_with_ref: true
tag_with_sha: true
always_pull: true

# docker build (master)
- name: docker - build master
if: github.ref == 'refs/heads/master'
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: l3uddz/nabarr/nabarr
dockerfile: docker/Dockerfile
tags: master
tag_with_sha: true
always_pull: true

# docker build (branch)
- name: docker - build other
if: startsWith(github.ref, 'refs/heads/master') == false
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: l3uddz/nabarr/nabarr
dockerfile: docker/Dockerfile
tag_with_ref: true
tag_with_sha: false
always_pull: true
54 changes: 54 additions & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Docker Cleanup

on: delete

jobs:
cleanup_branch:
if: startsWith(github.event.ref_type, 'branch') == true
runs-on: ubuntu-latest
steps:
- name: Sanitize branch docker tag
uses: frabert/replace-string-action@master
id: dockertag
with:
pattern: '[:\.\/]+'
string: "${{ github.event.ref }}"
replace-with: '-'
flags: 'g'

- name: query for package version id
uses: octokit/[email protected]
id: query_package_version
with:
query: |
query package($owner:String!,$repo:String!,$tag:String!) {
repository(owner: $owner, name: $repo) {
packages(names:[$repo], first:1) {
edges {
node {
id,
name,
version(version: $tag) {
id, version
}
}
}
}
}
}
owner: ${{ github.event.repository.owner.name }}
repo: ${{ github.event.repository.name }}
tag: ${{ steps.dockertag.outputs.replaced }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: parse package version id
id: package_version
run: echo "VERSION_ID=$(echo $json | jq -r $jsonpath)" >> $GITHUB_ENV
env:
json: ${{ steps.query_package_version.outputs.data }}
jsonpath: ".repository.packages.edges[].node.version.id"

- uses: actions/delete-package-versions@v1
with:
package-version-ids: '${{ env.VERSION_ID }}'
45 changes: 45 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# https://goreleaser.com
project_name: nabarr

# Build
builds:
-
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
main: ./cmd/nabarr
goarch:
- amd64
ldflags:
- -s -w
- -X "main.Version={{ .Version }}"
- -X "main.GitCommit={{ .ShortCommit }}"
- -X "main.Timestamp={{ .Timestamp }}"
flags:
- -trimpath

# Archive
archives:
-
name_template: "{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}"
format: "binary"

# Checksum
checksum:
name_template: "checksums.txt"
algorithm: sha512

# Snapshot
snapshot:
name_template: "{{ .Major }}.{{ .Minor }}.{{ .Patch }}-dev+{{ .ShortCommit }}"

# Changelog
changelog:
filters:
exclude:
- "^docs:"
- "^test:"
- "^Merge branch"
55 changes: 55 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.DEFAULT_GOAL := build
CMD := nabarr
TARGET := $(shell go env GOOS)_$(shell go env GOARCH)
DIST_PATH := dist
BUILD_PATH := ${DIST_PATH}/${CMD}_${TARGET}
GO_FILES := $(shell find . -path ./vendor -prune -or -type f -name '*.go' -print)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
TIMESTAMP := $(shell date +%s)
VERSION ?= 0.0.0-dev
CGO := 0

# Deps
.PHONY: check_goreleaser
check_goreleaser:
@command -v goreleaser >/dev/null || (echo "goreleaser is required."; exit 1)

.PHONY: vendor
vendor: ## Vendor files and tidy go.mod
go mod vendor
go mod tidy

.PHONY: vendor_update
vendor_update: ## Update vendor dependencies
go get -u ./...
${MAKE} vendor

.PHONY: build
build: vendor ${BUILD_PATH}/${CMD} ## Build application

# Binary
${BUILD_PATH}/${CMD}: ${GO_FILES} go.sum
@echo "Building for ${TARGET}..." && \
mkdir -p ${BUILD_PATH} && \
CGO_ENABLED=${CGO} go build \
-mod vendor \
-trimpath \
-ldflags "-s -w -X main.Version=${VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.Timestamp=${TIMESTAMP}" \
-o ${BUILD_PATH}/${CMD} \
./cmd/nabarr

.PHONY: release
release: check_goreleaser ## Generate a release, but don't publish
goreleaser --skip-validate --skip-publish --rm-dist

.PHONY: publish
publish: check_goreleaser ## Generate a release, and publish
goreleaser --rm-dist

.PHONY: snapshot
snapshot: check_goreleaser ## Generate a snapshot release
goreleaser --snapshot --skip-validate --skip-publish --rm-dist

.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
86 changes: 86 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cache

import (
"context"
"fmt"
"github.com/l3uddz/nabarr/logger"
"github.com/lefelys/state"
"github.com/rs/zerolog"
"github.com/xujiajun/nutsdb"
"time"
)

type Client struct {
log zerolog.Logger

st state.State
db *nutsdb.DB
}

func New(path string) (*Client, error) {
db, err := nutsdb.Open(nutsdb.Options{
Dir: path,
EntryIdxMode: nutsdb.HintKeyValAndRAMIdxMode,
SegmentSize: 8 * 1024 * 1024,
NodeNum: 1,
RWMode: nutsdb.FileIO,
SyncEnable: true,
StartFileLoadingMode: nutsdb.MMap,
})
if err != nil {
return nil, fmt.Errorf("open: %w", err)
}

log := logger.New("trace").With().Logger()

// start cleaner
st, tail := state.WithShutdown()
ticker := time.NewTicker(24 * time.Hour)
go func() {
for {
select {
case <-tail.End():
ticker.Stop()
tail.Done()
return
case <-ticker.C:
// clean cache
err := db.Update(func(tx *nutsdb.Tx) error {
return db.Merge()
})

switch {
case err == nil:
log.Info().Msg("Cleaned cache")
case err.Error() == "the number of files waiting to be merged is at least 2":
// there were no data files to be merged
default:
// unexpected error
log.Error().
Err(err).
Msg("Failed cleaning cache")
}
}
}
}()

return &Client{
log: log,
st: st,
db: db,
}, nil
}

func (c *Client) Close() error {
// shutdown cleaner
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := c.st.Shutdown(ctx); err != nil {
c.log.Error().
Err(err).
Msg("Failed shutting down cache cleaner gracefully")
}

// close cache
return c.db.Close()
}
Loading

0 comments on commit ce3807b

Please sign in to comment.