Skip to content

Commit

Permalink
feat: add etcd elector for gocron (#1)
Browse files Browse the repository at this point in the history
* feat: add etcd elector

* feat: add etcd elector

Signed-off-by: rfyiamcool <[email protected]>

* feat: add github workflow

Signed-off-by: rfyiamcool <[email protected]>

---------

Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
rfyiamcool authored Sep 25, 2023
1 parent a4c0fc3 commit 4ac835e
Show file tree
Hide file tree
Showing 14 changed files with 938 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: go-co-op
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

# Maintain Go dependencies
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
67 changes: 67 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '34 7 * * 1'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
33 changes: 33 additions & 0 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
on: [push]
name: golangci-lint
jobs:
golangci:
strategy:
matrix:
go-version:
- "1.20"
name: lint and test
runs-on: ubuntu-latest
services:
etcd:
image: bitnami/etcd:3.5.5
env:
ALLOW_NONE_AUTHENTICATION: yes
ETCD_ADVERTISE_CLIENT_URLS: http://127.0.0.1:2379
ETCDCTL_API: 3
ports:
- 2379:2379
- 2380:2380
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/[email protected]
with:
version: v1.51.2
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: test
run: make test
40 changes: 40 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Contributing to gocron

Thank you for coming to contribute to gocron! We welcome new ideas, PRs and general feedback.

## Reporting Bugs

If you find a bug then please let the project know by opening an issue after doing the following:

- Do a quick search of the existing issues to make sure the bug isn't already reported
- Try and make a minimal list of steps that can reliably reproduce the bug you are experiencing
- Collect as much information as you can to help identify what the issue is (project version, configuration files, etc)

## Suggesting Enhancements

If you have a use case that you don't see a way to support yet, we would welcome the feedback in an issue. Before opening the issue, please consider:

- Is this a common use case?
- Is it simple to understand?

You can help us out by doing the following before raising a new issue:

- Check that the feature hasn't been requested already by searching existing issues
- Try and reduce your enhancement into a single, concise and deliverable request, rather than a general idea
- Explain your own use cases as the basis of the request

## Adding Features

Pull requests are always welcome. However, before going through the trouble of implementing a change it's worth creating a bug or feature request issue.
This allows us to discuss the changes and make sure they are a good fit for the project.

Please always make sure a pull request has been:

- Unit tested with `make test`
- Linted with `make lint`
- Vetted with `make vet`
- Formatted with `make fmt` or validated with `make check-fmt`

## Writing Tests

Tests should follow the [table driven test pattern](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go). See other tests in the code base for additional examples.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: fmt check-fmt lint vet test

GO_PKGS := $(shell go list -f {{.Dir}} ./...)

fmt:
@go list -f {{.Dir}} ./... | xargs -I{} gofmt -w -s {}

lint:
@golangci-lint run

test:
@go test -v $(GO_FLAGS) -count=1 $(GO_PKGS)
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
# gocron-etcd-elector
# gocron-etcd-elector

## install

```
go get github.com/go-co-op/gocron-etcd-elector
```

## usage

Here is an example usage that would be deployed in multiple instances.

```go
package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/go-co-op/gocron"
elector "github.com/go-co-op/gocron-etcd-elector"
)

func main() {
cfg := elector.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
DialTimeout: 3 * time.Second,
}

el, err := elector.NewElector(context.Background(), cfg, elector.WithTTL(10))
if err != nil {
panic(err)
}

go func() {
for {
err := el.Start("/gocron/elector")
if err == elector.ErrClosed {
return
}

time.Sleep(1e9)
}
}()

s := gocron.NewScheduler(time.UTC)
s.WithDistributedElector(el)

s.Every("1s").Do(func() {
if el.IsLeader(context.TODO()) == nil {
fmt.Println("the current instance is leader")
} else {
fmt.Println("the current leader is", el.GetLeaderID())
}

fmt.Println("call 1s")
})

s.StartAsync()

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
<-c

fmt.Println("exit")
}
```
15 changes: 15 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Security Policy

## Supported Versions

The current plan is to maintain version 1 as long as possible incorporating any necessary security patches.

| Version | Supported |
| ------- | ------------------ |
| 1.x.x | :white_check_mark: |

## Reporting a Vulnerability

Vulnerabilities can be reported by [opening an issue](https://github.com/go-co-op/gocron/issues/new/choose) or reaching out on Slack: [<img src="https://img.shields.io/badge/gophers-gocron-brightgreen?logo=slack">](https://gophers.slack.com/archives/CQ7T0T1FW)

We will do our best to addrerss any vulnerabilities in an expeditious manner.
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'

services:
docker-etcd:
hostname: etcd
image: bitnami/etcd:3.5.5
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_ADVERTISE_CLIENT_URLS=http://127.0.0.1:2379
- ETCDCTL_API=3
ports:
- "2379:2379"
- "2380:2380"
Loading

0 comments on commit 4ac835e

Please sign in to comment.