Coder Social home page Coder Social logo

background-notifier's Introduction

Open another port for this background worker

echo "port 4000" | redis-server -

Send builk messages into redis

redis-cli -r 100 -p 4000 RPUSH resque:queue:slack '{"class":"notifier","args":["This is a test notification"]}'

Run the service

go run main.go -queues=notifier

Read profiling page on local after running the service

http://localhost:6060/debug/pprof/

background-notifier's People

Contributors

jywei avatar hexterch avatar

Stargazers

 avatar

Watchers

stewart avatar  avatar  avatar

background-notifier's Issues

Import slack notifier with goworker

package main

import (
	"github.com/benmanns/goworker"
	"fmt"
	"time"
	"github.com/parnurzeal/gorequest"
)

type Payload struct {
	Username  string `json:"username,omitempty"`
	IconEmoji string `json:"icon_emoji,omitempty"`
	Channel   string `json:"channel,omitempty"`
	Text      string `json:"text,omitempty"`
}


func redirectPolicyFunc(req gorequest.Request, via []gorequest.Request) error {
	return fmt.Errorf("Incorrect token (redirection)")
}


func send(webhookURL string, proxy string, payload Payload) []error {
	request := gorequest.New().Proxy(proxy)
	resp, _, err := request.
		Post(webhookURL).
		RedirectPolicy(redirectPolicyFunc).
		Send(payload).
		End()

	if err != nil {
		return err
	}
	if resp.StatusCode >= 400 {
		return []error{fmt.Errorf("Error sending msg. Status: %v", resp.Status)}
	}
	return nil
}

func notificationFunc(webhookURL string, response chan<- []error)  {
	payload := Payload{
		Text:      ":thinking_face:",
		Username:  "Hexter Bot",
		IconEmoji: ":hexter_is_ur_daddy:",
		Channel:   "#nuclear-testing-sites",
	}
	response <- send(webhookURL, "", payload)
}

func notificationWorker(queue string, args ...interface{}) error {
	webhookURL := "https://hooks.slack.com/services/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"
	responseChannel := make(chan []error)
	go notificationFunc(webhookURL, responseChannel)
	fmt.Printf("From %s, %v\n", queue, args)
	err := <- responseChannel
	return err[0]
}

func init() {
	settings := goworker.WorkerSettings{
		URI: "redis://localhost:6379/",
		Connections: 100,
		Queues:	[]string{"myqueue", "delimited", "queues"},
		UseNumber: true,
		ExitOnComplete: false,
		Concurrency: 2,
		Namespace: "resque:",
		Interval: 5.0,
	}
	goworker.SetSettings(settings)
	goworker.Register("MyClass", notificationWorker)
}

func main() {
	if err := goworker.Work(); err != nil {
		fmt.Println("Error", err)
	}
	fmt.Printf("Started on %v", time.Now().Format("2006-01-02 15:04:05"))
}

Worker channel pattern core

small core with gin API

package main

import (
"github.com/gin-gonic/gin"
)

func notificationWorker(response chan<- int) {
	// handle slack notification
	response <- 100
}

func main() {
	r := gin.Default()
	// notification api
	r.GET("/notification", func(c *gin.Context) {
		responseChannel := make(chan int, 10)
		go notificationWorker(responseChannel)
		response := <- responseChannel
		c.JSON(200, gin.H{
			"message": response,
		})
		close(responseChannel)
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.