{"id":13713896,"url":"https://github.com/phith0n/vindicator","last_synced_at":"2025-04-11T03:43:33.400Z","repository":{"id":61625448,"uuid":"540840297","full_name":"phith0n/vindicator","owner":"phith0n","description":"Vindicator is a lightweight Golang library that is designed to hold and check any blocking function. e.g. subprocess, network connection...","archived":false,"fork":false,"pushed_at":"2022-09-24T20:57:55.000Z","size":7,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-25T01:44:15.531Z","etag":null,"topics":["blocking","go","subprocess","sync"],"latest_commit_sha":null,"homepage":"https://github.com/phith0n/vindicator","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phith0n.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-24T13:34:47.000Z","updated_at":"2024-10-16T17:00:32.000Z","dependencies_parsed_at":"2022-10-18T18:00:25.076Z","dependency_job_id":null,"html_url":"https://github.com/phith0n/vindicator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phith0n%2Fvindicator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phith0n%2Fvindicator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phith0n%2Fvindicator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phith0n%2Fvindicator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phith0n","download_url":"https://codeload.github.com/phith0n/vindicator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248339289,"owners_count":21087213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blocking","go","subprocess","sync"],"created_at":"2024-08-02T23:01:47.175Z","updated_at":"2025-04-11T03:43:33.382Z","avatar_url":"https://github.com/phith0n.png","language":"Go","readme":"# Vindicator\n\nVindicator is a lightweight Golang library that is designed to hold and check any blocking function, e.g. subprocess,\nnetwork connection...\n\n## Contents\n\n- [Installation](#installation)\n- [Quick Start](#quick-start)\n- [API Reference](#api-reference)\n  - [Vindicator](#vindicator-struct)\n  - [Worker Interface](#worker-interface)\n  - [Event](#event)\n- [FAQ](#faq)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Installation\n\n```go\ngo get -u github.com/phith0n/vindicator\n```\n\n## Quick Start\n\nYou have to write a struct I call it \"Worker\", that implements `vindicator.Worker`:\n\n```go\ntype Worker interface {\n    Work(ctx context.Context) error // the worker() must be a blocking function\n    GetRunning() bool\n    SetRunning(bool)\n}\n```\n\nThere are 3 functions in the `Worker` interface:\n\n- `Work (ctx context.Context) error` this function must be a blocking function. the monitor will start a new worker if\n  this function exit unexpected.\n- `SetRunning(run bool)` this function should set the running status of the worker\n- `GetRunning() bool` this function should return current running status\n\nThe `Work` function accepts a context object, it must control the lifecycle of your worker.\n\nFor example, if you want to start a subprocess and check it regularly if it is still running, here is the struct\nimplement:\n\n```go\ntype ProcessWorker struct {\n    isRunning bool\n}\n\n// Work must be a blocking function\nfunc (pw *ProcessWorker) Work(ctx context.Context) error {\n    cmd := exec.CommandContext(ctx, \"sleep\", \"1h\")\n    if err := cmd.Start(); err != nil {\n        return err\n    }\n\n    if err := cmd.Wait(); err != nil {\n        return err\n    }\n\n    return nil\n}\n\nfunc (pw *ProcessWorker) SetRunning(run bool) {\n    pw.isRunning = run\n}\n\nfunc (pw *ProcessWorker) GetRunning() bool {\n    return pw.isRunning\n}\n```\n\nThen, use `Vindicator` to start and monitor the `ProcessWorker`:\n\n```go\nctx := context.Background()\nworker := ProcessWorker{}\nv := vindicator.NewVindicator(\u0026worker, 2)\n\n// you can use event listener to execute some callback function\nv.On(\"monitor:start\", func(v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"start monitor\")\n})\nv.On(\"monitor:working\", func (v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"process is working normally...\")\n})\nv.On(\"monitor:interrupt\", func (v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"process is stopped unexpected, try to restart it...\")\n})\nv.On(\"monitor:stop\", func (v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"stop monitor\")\n})\nv.On(\"worker:start\", func (v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"start worker\")\n})\nv.On(\"worker:stop\", func (v *vindicator.Vindicator, args ...interface{}) {\n    fmt.Println(\"stop worker\")\n})\n\n// run worker and monitor in background\ngo v.Start(ctx)\ngo v.Monitor(ctx)\n\n// to wait sometime...\ntimer := time.NewTimer(time.Second * 10)\n\u003c-timer.C\n\n// demonstrate how to stop the worker and the monitor manual\nv.Stop()\n```\n\nThis example checks the process running status every 2 seconds, and stop it after 10 seconds.\n\nThe output:\n\n```\nstart worker\nstart monitor\nprocess is working normally...\nprocess is working normally...\nprocess is working normally...\nprocess is working normally...\nprocess is working normally...\nstop monitor\nstop worker\n```\n\nThe full example code you can find [here](examples/subprocess_test.go).\n\n## API Reference\n\n### Vindicator Struct\n\nCreate a new `Vindicator`:\n\n```go\nv := vindicator.NewVindicator(\u0026worker, 2)\n```\n\nThe first argument is your custom `Worker` implements, the second argument is the monitor cycle time by seconds.\n\n### Worker Interface\n\n```go\ntype Worker interface {\n    Work(ctx context.Context) error // the worker() must be a blocking function\n    GetRunning() bool\n    SetRunning(bool)\n}\n```\n\n### Event\n\nThere are several events that you can listen and execute custom callback functions:\n\n- `monitor:start` trigger when monitor is started\n- `monitor:stop` trigger when monitor is stopped manual\n- `monitor:interrupt` trigger when monitor is stopped unexpected\n- `monitor:working` trigger when monitor works at cycle run\n- `worker:start` trigger when worker is started \n- `worker:stop` trigger when worker is stopped\n- `worker:error` trigger when an error is raised by worker\n\nUse `Vindicator.On` to register a listener:\n\n```go\ntype VindicatorFn func(v *Vindicator, args ...interface{})\n\nfunc (v *Vindicator) On(eventName string, callback VindicatorFn) {\n\t// ...\n}\n```\n\n## FAQ\n\n**When should I use this library?**\n\nYou can use **github.com/phith0n/vindicator** when you are going to run a blocking function and maintain its status. For example, the subprocess, the TCP long connection, the Websocket connection, and any other program like these.\n\n**Is there a document for this library?**\n\nNo yet. But there are only 100+ lines code for this project, you can kindly read the code and understand it by yourself.\n\n## Contributing\n\nIf you'd like to help out with the project. You can put up a Pull Request.\n\n## License\n\nThe Vindicator is open-sourced software licensed under the [MIT License](LICENSE).\n","funding_links":[],"categories":["Repositories"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphith0n%2Fvindicator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphith0n%2Fvindicator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphith0n%2Fvindicator/lists"}