tinyfile

package module
v0.0.0-...-1bbdd57 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: MIT Imports: 7 Imported by: 0

README

Go Reference GoTest

tinyfile is tiny service library

install

go get github.com/nc30/tinyfile

PidFile controll

tinyfile support simple pid control and logrotatable file object

example

package main

import (
	"fmt"
	"os"

	"github.com/nc30/tinyfile"
)

var PidFilePath = "/tmp/test.pid"

func main(){
	err := tinyfile.PidSet(PidFilePath)
	if err != nil {
		fmt.FPrintln(os.Stderr, err)
		os.Exit(1)
	}
	defer tinyfile.PidClean()


	/*****

		some process

	*****/
}

Rotatable file

create io.WriteCloser object at reopen of SIGHUP

package main

import (
	"context"
	"log"
	"os"

	"github.com/nc30/tinyfile"
)

var logPath = "/tmp/test.log"

func main() {
	// get rotatable io.WriteCloser object
	f, err := NewSync(logPath)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	// close file handler
	defer tinyfile.Close()

	// set outpu to log
	log.SetOutput(f)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// start watch signal of SIGHUP
	tinyfile.Watch(ctx)

	/*****

		some process

	*****/
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAnotherProcessRunning = errors.New("pid file is aleady exist")
	ErrAleadySetPid          = errors.New("aleady seted pid on this process")

	PidFileFlg                    = os.O_CREATE | os.O_WRONLY
	PidFilePermission os.FileMode = 0664
)
View Source
var (
	FileFlg                    = os.O_CREATE | os.O_WRONLY | os.O_APPEND
	FilePermission os.FileMode = 0664
)

Functions

func Close

func Close()

Close is close all files

func FileExist

func FileExist(path string) bool

FileExist check path is file and exist true is only file exist and type is file

func PidClean

func PidClean() error

PidClean delete pid file of created by PidSet()

func PidSet

func PidSet(path string) error

PidSet create pid file.

require run PidClean() on process end. this function work only once.

func Watch

func Watch(ctx context.Context)

watch SIGHUP

func WatchWithSignal

func WatchWithSignal(ctx context.Context, sig ...os.Signal)

watch signal

Types

type OpenFileFunc

type OpenFileFunc func(string, int, os.FileMode) (*os.File, error)
var OpenFile OpenFileFunc = os.OpenFile

type SyncRepo

type SyncRepo struct {
	// contains filtered or unexported fields
}

SyncRepo is TinyFile struct controller. that wach some signal and ma

func NewRepo

func NewRepo() *SyncRepo

func (*SyncRepo) Close

func (sr *SyncRepo) Close()

Close is close all management under TinyWriter

func (*SyncRepo) New

func (sr *SyncRepo) New(path string) (*TinyWriter, error)

create SyncRepository

func (*SyncRepo) ReOpen

func (sr *SyncRepo) ReOpen() []error

ReOpen all management under TinyWriter

func (*SyncRepo) Watch

func (sr *SyncRepo) Watch(ctx context.Context)

Watch start SIGHUP watchng process

func (*SyncRepo) WatchWithSignal

func (sr *SyncRepo) WatchWithSignal(ctx context.Context, sig ...os.Signal)

WatchWithSignal start signal watching function

type TinyWriter

type TinyWriter struct {
	// contains filtered or unexported fields
}

TinyWriter is zapcore.WriteSyncer

func NewWriter

func NewWriter(path string) (*TinyWriter, error)

create sync object

func (*TinyWriter) Close

func (s *TinyWriter) Close() error

func (*TinyWriter) ReOpen

func (s *TinyWriter) ReOpen() error

func (*TinyWriter) Sync

func (s *TinyWriter) Sync() error

func (*TinyWriter) Write

func (s *TinyWriter) Write(p []byte) (n int, err error)

write to file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL