diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..76970fb --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,69 @@ +# Code of Conduct - looper + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to make participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behaviour that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologising to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behaviour include: + +* The use of sexualised language or imagery, and sexual attention or advances +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying and enforcing our standards of +acceptable behaviour and will take appropriate and fair corrective action in +response to any instances of unacceptable behaviour. + +Project maintainers have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, or to ban +temporarily or permanently any contributor for other behaviours that they deem +inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behaviour may be +reported to the community leaders responsible for enforcement at . +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant](https://contributor-covenant.org/), version +[1.4](https://www.contributor-covenant.org/version/1/4/code-of-conduct/code_of_conduct.md) and +[2.0](https://www.contributor-covenant.org/version/2/0/code_of_conduct/code_of_conduct.md), +and was generated by [contributing-gen](https://github.com/bttger/contributing-gen). diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..02d0752 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,106 @@ +# Contributing to looper + +First off, thanks for taking the time to contribute! ❤️ + +All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉 + +> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: +> - Star the project +> - Tweet about it +> - Refer this project in your project's readme +> - Mention the project at local meetups and tell your friends/colleagues + +## Table of Contents + +- [Code of Conduct](#code-of-conduct) +- [I Have a Question](#i-have-a-question) + - [I Want To Contribute](#i-want-to-contribute) + - [Reporting Bugs](#reporting-bugs) + - [Suggesting Enhancements](#suggesting-enhancements) + - [Your First Code Contribution](#your-first-code-contribution) + - [Improving The Documentation](#improving-the-documentation) + +## Code of Conduct + +This project and everyone participating in it is governed by the +[looper Code of Conduct](https://github.com/golang-cz/looper/blob/master/CODE_OF_CONDUCT.md). +By participating, you are expected to uphold this code. Please report unacceptable behaviour +to . + + +## I Have a Question + +> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/golang-cz/looper/blob/master/README.md). + +Before you ask a question, it is best to search for existing [Issues](https://github.com/golang-cz/looper/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first. + +If you then still feel the need to ask a question and need clarification, we recommend the following: + +- Open an [Issue](https://github.com/golang-cz/looper/issues/new). +- Provide as much context as you can about what you're running into. +- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant. + +We will then take care of the issue as soon as possible. + +## I Want To Contribute + +> ### Legal Notice +> When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project licence. + +### Reporting Bugs + +#### Before Submitting a Bug Report + +A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. + +- Make sure that you are using the latest version. +- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/golang-cz/looper/blob/master/README.md). If you are looking for support, you might want to check [this section](#i-have-a-question)). +- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/golang-cz/looper/issues?q=label%3Abug). +- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue. +- Collect information about the bug: + - Stack trace (Traceback) + - OS, Platform and Version (Windows, Linux, macOS, x86, ARM) + - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. + - Possibly your input and the output + - Can you reliably reproduce the issue? And can you also reproduce it with older versions? + +#### How Do I Submit a Good Bug Report? + +> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to . + +We use GitHub issues to track bugs and errors. If you run into an issue with the project: + +- Open an [Issue](https://github.com/golang-cz/looper/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.) +- Explain the behaviour you would expect and the actual behaviour. +- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. +- Provide the information you collected in the previous section. + +Once it's filed: + +- The project team will label the issue accordingly. +- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. +- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution). + +### Suggesting Enhancements + +This section guides you through submitting an enhancement suggestion for looper, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. + +#### Before Submitting an Enhancement + +- Make sure that you are using the latest version. +- Read the [documentation](https://github.com/golang-cz/looper/blob/master/README.md) carefully and find out if the functionality is already covered, maybe by an individual configuration. +- Perform a [search](https://github.com/golang-cz/looper/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. +- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library. + +#### How Do I Submit a Good Enhancement Suggestion? + +Enhancement suggestions are tracked as [GitHub issues](https://github.com/golang-cz/looper/issues). + +- Use a **clear and descriptive title** for the issue to identify the suggestion. +- Provide a **step-by-step description of the suggested enhancement** in as many details as possible. +- **Describe the current behaviour** and **explain which behaviour you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. +- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. +- **Explain why this enhancement would be useful** to most looper users. You may also want to point out the other projects that solved it better and which could serve as inspiration. + +## Attribution +This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)! diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..7589815 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,31 @@ +--- +name: Bug report +about: Create a report to help us improve +title: "[BUG] - " +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. Linux] + - Version [e.g. 1.21.0] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..fdc4f3c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: "[FEATURE] - " +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..1963abb --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,8 @@ +### Description +Please explain the changes you made here. + +### Checklist +- [ ] Code compiles correctly +- [ ] Created tests which fail without the change (if possible) +- [ ] All tests passing +- [ ] Extended the README / documentation, if necessary diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..ea57d8a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,25 @@ +name: Tests + +on: + push: + branches: + - master + pull_request: + +jobs: + test: + name: Test check + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21.0 + + - name: Run tests and coverage + run: | + go test -v -coverprofile=coverage.txt -covermode=atomic ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c57100a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..f335ed2 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# ➰ looper - Looper for jobs with redis lock + [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/golang-cz/looper/blob/master/LICENSE) + [![Go Report Card](https://goreportcard.com/badge/github.com/golang-cz/looper)](https://goreportcard.com/report/github.com/golang-cz/looper) + [![Go Reference](https://pkg.go.dev/badge/github.com/golang-cz/looper.svg)](https://pkg.go.dev/github.com/golang-cz/looper) + +`looper` is a for running jobs in loop, it supports redis locking for using looper in mulitple instances + +TODO: everything diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4d81234 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module looper + +go 1.21.3 + +require ( + github.com/go-redsync/redsync/v4 v4.11.0 + github.com/redis/go-redis/v9 v9.3.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d004ffb --- /dev/null +++ b/go.sum @@ -0,0 +1,31 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4= +github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= +github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg= +github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w= +github.com/go-redsync/redsync/v4 v4.11.0 h1:OPEcAxHBb95EzfwCKWM93ksOwHd5bTce2BD4+R14N6k= +github.com/go-redsync/redsync/v4 v4.11.0/go.mod h1:ZfayzutkgeBmEmBlUR3j+rF6kN44UUGtEdfzhBFZTPc= +github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws= +github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0= +github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/redis/rueidis v1.0.19 h1:s65oWtotzlIFN8eMPhyYwxlwLR1lUdhza2KtWprKYSo= +github.com/redis/rueidis v1.0.19/go.mod h1:8B+r5wdnjwK3lTFml5VtxjzGOQAC+5UmujoD12pDrEo= +github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203 h1:QVqDTf3h2WHt08YuiTGPZLls0Wq99X9bWd0Q5ZSBesM= +github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203/go.mod h1:oqN97ltKNihBbwlX8dLpwxCl3+HnXKV/R0e+sRLd9C8= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= diff --git a/looper.go b/looper.go new file mode 100644 index 0000000..9815d0e --- /dev/null +++ b/looper.go @@ -0,0 +1,429 @@ +package looper + +import ( + "context" + "errors" + "fmt" + "strings" + "sync" + "time" + + "github.com/go-redsync/redsync/v4" + "github.com/redis/go-redis/v9" +) + +// Panic handler +type PanicHandlerFunc func(jobName string, recoverData interface{}) + +var ( + panicHandler PanicHandlerFunc + panicHandlerMutex = sync.RWMutex{} +) + +func SetPanicHandler(handler PanicHandlerFunc) { + panicHandlerMutex.Lock() + defer panicHandlerMutex.Unlock() + panicHandler = handler +} + +// Looper +type Looper struct { + running bool + jobs []*Job + startupTime time.Duration + hooks hooks + mu sync.RWMutex + redisClient *redis.Client +} + +type ( + HookBeforeJob func(jobName string) + HookAfterJob func(jobName string, duration time.Duration) + HookAfterJobError func(jobName string, duration time.Duration, err error) +) + +type hooks struct { + beforeJob HookBeforeJob + afterJob HookAfterJob + afterJobError HookAfterJobError +} + +type Config struct { + // Startup time ensuring a consistent delay between registered jobs on start of looper. + // + // StartupTime = 1 second; 5 registered jobs; Jobs would be initiated + // with 200ms delay + StartupTime time.Duration + + RedisClient *redis.Client +} + +type JobFn func(ctx context.Context) error + +type Job struct { + // Job function which get triggered by looper. + JobFn JobFn + + // Name of the job. + Name string + + // Timeout for job, maximum time, the job can run, after timeout the job get killed. + Timeout time.Duration + + // Wait duration before next job execution after successful execution of previous job. + WaitAfterSuccess time.Duration + + // Wait duration before next job execution after unsuccessful execution of previous job. + WaitAfterError time.Duration + + // If job is Active, and can be started. + Active bool + + // If job is started. + Started bool + + // If job is currently running. + Running bool + + // Last time the job ran. + LastRun time.Time + + // Count of successful job runs. + RunCountSuccess uint64 + + // Count of unsuccessful job runs. + RunCountError uint64 + + // Copy of last error, that occured. + LastError error + + // Hook function before job runs. + BeforeJob HookBeforeJob + + // Hook function after job runs successfully. + AfterJob HookAfterJob + + // Hook function after job runs unsuccessfully. + AfterJobError HookAfterJobError + + // If the job should use redis locker + WithLocker bool + + // Locker + locker *locker + + // Context cancel + contextCancel context.CancelFunc + + mu sync.RWMutex +} + +func New(config Config) *Looper { + return &Looper{ + jobs: []*Job{}, + startupTime: setDefaultDuration(config.StartupTime, time.Second), + hooks: hooks{ + beforeJob: func(jobName string) {}, + afterJob: func(jobName string, duration time.Duration) {}, + afterJobError: func(jobName string, duration time.Duration, err error) {}, + }, + redisClient: config.RedisClient, + } +} + +func (l *Looper) RegisterHooks( + beforeJob HookBeforeJob, + afterJob HookAfterJob, + afterJobError HookAfterJobError, +) { + l.hooks.beforeJob = beforeJob + l.hooks.afterJobError = afterJobError + l.hooks.afterJob = afterJob +} + +func (l *Looper) AddJob(ctx context.Context, jobInput *Job) error { + if jobInput == nil { + return nil + } + + l.mu.Lock() + defer l.mu.Unlock() + + beforeJob := jobInput.BeforeJob + if beforeJob == nil { + beforeJob = l.hooks.beforeJob + } + + afterJob := jobInput.AfterJob + if afterJob == nil { + afterJob = l.hooks.afterJob + } + + afterJobError := jobInput.AfterJobError + if afterJobError == nil { + afterJobError = l.hooks.afterJobError + } + + j := &Job{ + JobFn: jobInput.JobFn, + Name: l.uniqueName(jobInput.Name), + Timeout: setDefaultDuration(jobInput.Timeout, time.Minute), + WaitAfterSuccess: setDefaultDuration(jobInput.WaitAfterSuccess, time.Second), + WaitAfterError: setDefaultDuration(jobInput.WaitAfterError, time.Second), + Active: true, + BeforeJob: beforeJob, + AfterJob: afterJob, + AfterJobError: afterJobError, + WithLocker: jobInput.WithLocker, + mu: sync.RWMutex{}, + } + + if jobInput.WithLocker && l.redisClient != nil { + locker, err := newRedisLocker(ctx, l.redisClient, redsync.WithTries(1), redsync.WithExpiry(j.Timeout+time.Second)) + if err != nil { + return fmt.Errorf("new redis locker for job %s: %w", j.Name, err) + } + + j.locker = &locker + } + + l.jobs = append(l.jobs, j) + + return nil +} + +func setDefaultDuration(duration time.Duration, defaultDuration time.Duration) time.Duration { + if duration == time.Duration(0) { + return defaultDuration + } + + return duration +} + +func (l *Looper) uniqueName(jobInputName string) string { + var counter int + for _, j := range l.jobs { + parts := strings.Split(j.Name, "-") + jobName := strings.Join(parts[:len(parts)-1], "-") + if jobName == jobInputName { + counter++ + } + } + + return fmt.Sprintf("%s-%v", jobInputName, counter) +} + +func (l *Looper) StartJobByName(jobName string) error { + l.mu.RLock() + defer l.mu.RUnlock() + + var found bool + for _, j := range l.jobs { + j.mu.Lock() + parts := strings.Split(j.Name, "-") + name := strings.Join(parts[:len(parts)-1], "-") + if name == jobName { + found = true + if j.Active && !j.Started { + j.Started = true + go j.start() + } + } + + j.mu.Unlock() + if found { + break + } + } + + if !found { + return fmt.Errorf("job with name(%s) not found", jobName) + } + + return nil +} + +func (l *Looper) Start() { + l.mu.Lock() + defer l.mu.Unlock() + + if !l.running { + go l.startJobs() + l.running = true + } +} + +func (l *Looper) startJobs() { + if len(l.jobs) == 0 { + return + } + + delay := time.Duration(l.startupTime) / time.Duration(len(l.jobs)) + for _, j := range l.jobs { + j.mu.Lock() + if j.Active && !j.Started { + j.Started = true + go j.start() + time.Sleep(delay) + } + + j.mu.Unlock() + } +} + +func (l *Looper) Stop() { + l.mu.Lock() + for _, j := range l.jobs { + j.mu.Lock() + j.Started = false + if j.contextCancel != nil { + j.contextCancel() + } + + j.mu.Unlock() + } + + l.mu.Unlock() + + for { + rj := l.runningJobs() + if rj == 0 { + break + } + + time.Sleep(time.Millisecond * 200) + } + + l.mu.Lock() + l.running = false + l.mu.Unlock() +} + +func (j *Job) start() { + defer func() { + j.mu.Lock() + j.Started = false + j.contextCancel = nil + j.mu.Unlock() + }() + + var errLock error + var err error + ctxLock := context.Background() + + for { + j.mu.RLock() + if !j.Active || !j.Started { + j.mu.RUnlock() + break + } + j.mu.RUnlock() + + ctx, cancel := context.WithTimeout(context.Background(), j.Timeout) + + j.mu.Lock() + j.contextCancel = cancel + j.Running = true + + var redisLock lock + + if j.WithLocker { + lo := *j.locker + redisLock, errLock = lo.lock(ctxLock, j.Name) + if errors.Is(errLock, ErrFailedToObtainLock) { + // time.Sleep(j.WaitAfterSuccess) + time.Sleep(time.Duration(time.Second)) + j.Running = false + cancel() + j.mu.Unlock() + continue + } + + if errLock != nil { + err = errLock + } + } + + j.BeforeJob(j.Name) + j.mu.Unlock() + + start := time.Now() + if err == nil { + err = j.Run(ctx) + } + + if j.WithLocker && errLock == nil { + errLock = redisLock.unlock(ctxLock) + } + + if err != nil || errLock != nil { + if err != nil { + j.AfterJobError(j.Name, time.Since(start), err) + } else { + j.AfterJobError(j.Name, time.Since(start), errLock) + } + + time.Sleep(j.WaitAfterError) + } else { + j.AfterJob(j.Name, time.Since(start)) + time.Sleep(j.WaitAfterSuccess) + } + + cancel() + } +} + +func (j *Job) Run(ctx context.Context) (err error) { + defer func() { + j.mu.Lock() + defer j.mu.Unlock() + + j.LastRun = time.Now() + j.Running = false + + r := recover() + if r != nil { + recErr, ok := r.(error) + if ok { + err = recErr + } else { + err = fmt.Errorf("%v", r) + } + + if panicHandler != nil { + panicHandler(j.Name, r) + } + } + + if err != nil { + j.RunCountError++ + j.LastError = err + } else { + j.RunCountSuccess++ + } + }() + + err = j.JobFn(ctx) + + return err +} + +func (l *Looper) runningJobs() (count int) { + l.mu.Lock() + defer l.mu.Unlock() + + for _, j := range l.jobs { + j.mu.RLock() + if j.Running { + count++ + } + + j.mu.RUnlock() + } + + return count +} + +func (l *Looper) Jobs() []*Job { + return l.jobs +} diff --git a/redis.go b/redis.go new file mode 100644 index 0000000..14133e5 --- /dev/null +++ b/redis.go @@ -0,0 +1,82 @@ +package looper + +// inpired by https://github.com/go-co-op/gocron-redis-lock + +import ( + "context" + "errors" + "fmt" + + "github.com/go-redsync/redsync/v4" + "github.com/go-redsync/redsync/v4/redis/goredis/v9" + "github.com/redis/go-redis/v9" +) + +var ( + ErrFailedToConnectToRedis = errors.New("looper - failed to connect to redis") + ErrFailedToObtainLock = errors.New("looper - failed to obtain lock") + ErrFailedToReleaseLock = errors.New("looper - failed to release lock") +) + +type locker interface { + lock(ctx context.Context, key string) (lock, error) +} + +type lock interface { + unlock(ctx context.Context) error +} + +func newRedisLocker(ctx context.Context, r redis.UniversalClient, options ...redsync.Option) (locker, error) { + err := r.Ping(ctx).Err() + if err != nil { + return nil, fmt.Errorf("%s: %w", ErrFailedToConnectToRedis, err) + } + + return newLocker(r, options...), nil +} + +func newLocker(r redis.UniversalClient, options ...redsync.Option) locker { + pool := goredis.NewPool(r) + rs := redsync.New(pool) + return &redisLocker{rs: rs, options: options} +} + +var _ locker = (*redisLocker)(nil) + +type redisLocker struct { + rs *redsync.Redsync + options []redsync.Option +} + +func (r *redisLocker) lock(ctx context.Context, key string) (lock, error) { + mu := r.rs.NewMutex(key, r.options...) + err := mu.LockContext(ctx) + if err != nil { + return nil, ErrFailedToObtainLock + } + + rl := &redisLock{ + mu: mu, + } + + return rl, nil +} + +var _ lock = (*redisLock)(nil) + +type redisLock struct { + mu *redsync.Mutex +} + +func (r *redisLock) unlock(ctx context.Context) error { + unlocked, err := r.mu.UnlockContext(ctx) + if err != nil { + return ErrFailedToReleaseLock + } + + if !unlocked { + return ErrFailedToReleaseLock + } + + return nil +}