Skip to content

Commit

Permalink
added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
padaliyajay committed Dec 25, 2024
1 parent 845eaf5 commit fbf67cf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Asynchook allows you run task in background by creating a hook which call your u

### Installation
```bash
wget https://github.com/padaliyajay/asynchook/releases/download/v1.0.1/asynchook_1.0.1_amd64.deb
dpkg -i asynchook_1.0.0_amd64.deb
wget https://github.com/padaliyajay/asynchook/releases/download/v1.0.2/asynchook_1.0.2_amd64.deb
dpkg -i asynchook_1.0.2_amd64.deb
systemctl enable asynchook
```

Expand All @@ -16,10 +16,18 @@ systemctl start asynchook
### Configuration
File: /etc/asynchook/config.yaml
```yaml
# Redis configuration
# Asynchook uses redis for message queueing
redis:
addr: localhost:6379
db: 0
password:

# Asynchook error log
# logFile: /var/log/asynchook.log

# Asynchook channels
# You can add multiple channels with different ratelimits
channels:
- name: default
ratelimit: 2/s # rate limit for this channel Ex. 2/s, 60/m, 300/h
Expand All @@ -28,10 +36,10 @@ channels:
### Usage
##### Send event to redis
```bash
HSET asynchook:1001 id 1001 url http://localhost:8080/mail payload '[YOUR JSON TEXT]' timestamp 1600000000 secret '[Your Secret]'
HSET asynchook:1001 id 1001 url http://localhost:8080/mail payload '[YOUR JSON TEXT]' secret '[Your Secret]'
ZADD asynchooks:default 1 1001
```
Here Id and URL are mandatory fields. But payload, timestamp and secret are optional.
Here Id and URL are mandatory fields. But payload and secret are optional.

## License
MIT
2 changes: 1 addition & 1 deletion build-deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e

# Variables
APP_NAME="asynchook" # Name of your application
VERSION="1.0.1" # Version of your application
VERSION="1.0.2" # Version of your application
ARCH="amd64" # Architecture (amd64, arm64, all, etc.)
BUILD_DIR="./build" # Temporary build directory
PACKAGE_DIR="./package" # Package structure directory
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Password string `yaml:"password"`
DB int `yaml:"db"`
} `yaml:"redis"`
LogFile string `yaml:"logFile"`
Channels []struct {
Name string `yaml:"name"`
Ratelimit string `yaml:"ratelimit"`
Expand Down Expand Up @@ -56,6 +57,7 @@ func createConfigFile(fp string) error {
Password: "",
DB: 0,
},
LogFile: "/var/log/asynchook.log",
Channels: []struct {
Name string `yaml:"name"`
Ratelimit string `yaml:"ratelimit"`
Expand Down
11 changes: 9 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Redis configuration
# Asynchook uses redis for message queueing
redis:
addr: localhost:6379
db: 0
password:

# Asynchook error log
# logFile: /var/log/asynchook.log

# Asynchook channels
# You can add multiple channels with different ratelimits
channels:
- name: default
ratelimit: 2/s

ratelimit: 2/s
3 changes: 2 additions & 1 deletion hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func (s *HookManager) Process(hook *HookEvent) error {
s.rl.Acquire()
err := hook.Process()
if err != nil {
fmt.Println(err)
log.Println(err)
}

return err
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"sync"
Expand All @@ -15,8 +16,18 @@ func main() {
flag.Parse()

if config, err := LoadConfig(config_file); err != nil {
panic(err)
log.Fatal(err)
} else {
// Create log file if specified and set log output to it
if config.LogFile != "" {
f, err := os.OpenFile(config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
}

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

broker := NewRedisBroker(ctx, config.Redis.Addr, config.Redis.Password, config.Redis.DB)
Expand Down
7 changes: 4 additions & 3 deletions rateLimiter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"strconv"
"strings"
"sync"
Expand All @@ -19,12 +20,12 @@ type RateLimiter struct {
func NewRateLimiter(rateLimit string) *RateLimiter {
parts := strings.Split(rateLimit, "/")
if len(parts) != 2 {
panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
log.Panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
}

limit, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
log.Panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
}

var duration time.Duration
Expand All @@ -36,7 +37,7 @@ func NewRateLimiter(rateLimit string) *RateLimiter {
case "h":
duration = time.Hour
default:
panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
log.Panic("invalid rate limit. Must be in the format of <limit>/<time>. Ex. 20/m 30/s 300/h")
}

rl := &RateLimiter{
Expand Down
3 changes: 2 additions & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"log"
"time"

"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (b *RedisBroker) Run(manager *HookManager) {

hook, err := b.getHook(id)
if err != nil {
fmt.Println(err)
log.Println(err)
continue
}

Expand Down

0 comments on commit fbf67cf

Please sign in to comment.