robin

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 5 Imported by: 2

README

robin

GitHub FOSSA Status Go Report Card build-test codecov

Features

Fiber

  • GoroutineSingle - a fiber backed by a dedicated goroutine. Every job is executed by a goroutine.
  • GoroutineMulti - a fiber backed by more goroutine. Each job is executed by a new goroutine.

Channels

  • Channels callback is executed for each message received.

Cron

Golang job scheduling for humans. It is inspired by schedule.

Usage

Quick Start

1.Install

  go get github.com/jiansoft/robin

2.Use examples

import (
    "log"
    "time"
    
    "github.com/jiansoft/robin"
)

func main() {
    //The method is going to execute only once after 2000 ms.
    robin.Delay(2000).Do(runCron, "a Delay 2000 ms")
    
    minute := 11
    second := 50
    
    //Every Friday is going to execute once at 14:11:50 (HH:mm:ss).
    robin.EveryFriday().At(14, minute, second).Do(runCron, "Friday")

    //Every N day  is going to execute once at 14:11:50(HH:mm:ss)
    robin.Every(1).Days().At(14, minute, second).Do(runCron, "Days")

    //Every N hours is going to execute once at 11:50:00(HH:mm:ss).
    robin.Every(1).Hours().At(0, minute, second).Do(runCron, "Every 1 Hours")

    //Every N minutes is going to execute once at 50(ss).
    robin.Every(1).Minutes().At(0, 0, second).Do(runCron, "Every 1 Minutes")

    //Every N seconds is going to execute once
    robin.Every(10).Seconds().Do(runCron, "Every 10 Seconds")
    
    p1 := player{Nickname: "Player 1"}
    p2 := player{Nickname: "Player 2"}
    p3 := player{Nickname: "Player 3"}
    p4 := player{Nickname: "Player 4"}
    
    //Create a channel
    channel := robin.NewChannel()
    
    //Four player subscribe the channel
    channel.Subscribe(p1.eventFinalBossResurge)
    channel.Subscribe(p2.eventFinalBossResurge)
    p3Subscribe := channel.Subscribe(p3.eventFinalBossResurge)
    p4Subscribe := channel.Subscribe(p4.eventFinalBossResurge)
    
    //Publish a message to the channel and then the four subscribers of the channel will 
    //receives the message each that "The boss resurge first." .
    channel.Publish("The boss resurge first.")
    
    //Unsubscribe p3 and p4 from the channel.
    channel.Unsubscribe(p3Subscribe)
    p4Subscribe.Unsubscribe()
    
    //This time just p1 and p2 receives the message that "The boss resurge second.".
    channel.Publish("The boss resurge second.")
    
    //Unsubscribe all subscribers from the channel
    channel.Clear()
    
    //The channel is empty so no one can receive the message
    channel.Publish("The boss resurge third.")
}

func runCron(s string) {
    log.Printf("I am %s CronTest %v\n", s, time.Now())
}

type player struct {
	Nickname string
}
func (p player) eventFinalBossResurge(someBossInfo string) {
	log.Printf("%s receive a message : %s", p.Nickname, someBossInfo)
}

More example

License

Copyright (c) 2017

Released under the MIT license:

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int) int

Abs Returns the absolute value of a specified int number.

func AbsForInt64 added in v1.0.9

func AbsForInt64(n int64) int64

AbsForInt64 Returns the absolute value of a specified int64 number.

Types

type Channel added in v1.0.10

type Channel struct {
	sync.Map
}

Channel is a struct that has a member variable to store subscribers

func NewChannel

func NewChannel() *Channel

NewChannel new a Channel instance

func (*Channel) Clear added in v1.0.10

func (c *Channel) Clear()

Clear empty the subscribers

func (*Channel) Count added in v1.0.10

func (c *Channel) Count() int

Count returns a number that how many subscribers in the Channel.

func (*Channel) Publish added in v1.0.10

func (c *Channel) Publish(msg ...any)

Publish a message to all subscribers

func (*Channel) Subscribe added in v1.0.10

func (c *Channel) Subscribe(taskFunc any, params ...any) *Subscriber

Subscribe to register a receiver to receive the Channel's message

func (*Channel) Unsubscribe added in v1.0.10

func (c *Channel) Unsubscribe(subscriber any)

Unsubscribe remove the subscriber from the channel

type ConcurrentBag added in v1.1.1

type ConcurrentBag struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ConcurrentBag represents a thread-safe, unordered collection of element.

func NewConcurrentBag added in v1.1.1

func NewConcurrentBag() *ConcurrentBag

NewConcurrentBag new a ConcurrentStack instance

func (*ConcurrentBag) Add added in v1.1.1

func (cb *ConcurrentBag) Add(element any)

Add an element to the ConcurrentBag.

func (*ConcurrentBag) Clear added in v1.1.1

func (cb *ConcurrentBag) Clear()

Clear remove all element in the ConcurrentBag.

func (*ConcurrentBag) Len added in v1.1.1

func (cb *ConcurrentBag) Len() int

Len gets the number of elements contained in the ConcurrentBag.

func (*ConcurrentBag) ToArray added in v1.1.1

func (cb *ConcurrentBag) ToArray() (elements []any)

ToArray copies the ConcurrentBag elements to a new array.

func (*ConcurrentBag) TryTake added in v1.1.1

func (cb *ConcurrentBag) TryTake() (any, bool)

TryTake attempts to remove and return an element from the ConcurrentBag

type ConcurrentQueue

type ConcurrentQueue struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ConcurrentQueue represents a thread-safe first in-first out (FIFO) collection.

func NewConcurrentQueue

func NewConcurrentQueue() *ConcurrentQueue

NewConcurrentQueue new a ConcurrentQueue instance

func (*ConcurrentQueue) Clear added in v1.1.1

func (c *ConcurrentQueue) Clear()

Clear remove all element in the ConcurrentQueue.

func (*ConcurrentQueue) Enqueue

func (c *ConcurrentQueue) Enqueue(element any)

Enqueue adds an object to the end of the ConcurrentQueue.

func (*ConcurrentQueue) Len added in v1.1.1

func (c *ConcurrentQueue) Len() int

Len gets the number of elements contained in the ConcurrentQueue.

func (*ConcurrentQueue) ToArray added in v1.1.1

func (c *ConcurrentQueue) ToArray() (elements []any)

ToArray copies the elements stored in the ConcurrentQueue to a new array.

func (*ConcurrentQueue) TryDequeue

func (c *ConcurrentQueue) TryDequeue() (any, bool)

TryDequeue tries to remove and return the element at the beginning of the ConcurrentQueue.

func (*ConcurrentQueue) TryPeek

func (c *ConcurrentQueue) TryPeek() (any, bool)

TryPeek tries to return an element from the beginning of the ConcurrentQueue without removing it.

type ConcurrentStack added in v1.1.1

type ConcurrentStack struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ConcurrentStack represents a thread-safe last in-first out (LIFO) collection.

func NewConcurrentStack added in v1.1.1

func NewConcurrentStack() *ConcurrentStack

NewConcurrentStack new a ConcurrentStack instance

func (*ConcurrentStack) Clear added in v1.1.1

func (c *ConcurrentStack) Clear()

Clear remove all element in the ConcurrentStack.

func (*ConcurrentStack) Len added in v1.1.1

func (c *ConcurrentStack) Len() int

Len gets the number of elements contained in the ConcurrentStack.

func (*ConcurrentStack) Push added in v1.1.1

func (c *ConcurrentStack) Push(element any)

Push adds an object to the end of the ConcurrentStack.

func (*ConcurrentStack) ToArray added in v1.1.1

func (c *ConcurrentStack) ToArray() (elements []any)

ToArray copies the elements stored in the ConcurrentStack to a new array.

func (*ConcurrentStack) TryPeek added in v1.1.1

func (c *ConcurrentStack) TryPeek() (any, bool)

TryPeek tries to return an element from the beginning of the ConcurrentStack without removing it.

func (*ConcurrentStack) TryPop added in v1.1.1

func (c *ConcurrentStack) TryPop() (any, bool)

TryPop attempts to pop and return the object at the top of the

type Disposable

type Disposable interface {
	Dispose()
}

Disposable an interface just has only one function

type Fiber

type Fiber interface {
	Start()
	Stop()
	Dispose()
	Enqueue(taskFunc any, params ...any)
	EnqueueWithTask(task Task)
	Schedule(firstInMs int64, taskFunc any, params ...any) (d Disposable)
	ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFunc any, params ...any) (d Disposable)
}

Fiber define some function

type GoroutineMulti

type GoroutineMulti struct {
	// contains filtered or unexported fields
}

GoroutineMulti a fiber backed by more goroutine. Each job is executed by a new goroutine.

func NewGoroutineMulti

func NewGoroutineMulti() *GoroutineMulti

NewGoroutineMulti create a GoroutineMulti instance

func (*GoroutineMulti) Dispose

func (g *GoroutineMulti) Dispose()

Dispose stop the fiber and release resource

func (*GoroutineMulti) Enqueue

func (g *GoroutineMulti) Enqueue(taskFunc any, params ...any)

Enqueue use the fiber to execute a task

func (*GoroutineMulti) EnqueueWithTask

func (g *GoroutineMulti) EnqueueWithTask(task Task)

EnqueueWithTask use the fiber to execute a task

func (*GoroutineMulti) Schedule

func (g *GoroutineMulti) Schedule(firstInMs int64, taskFunc any, params ...any) (d Disposable)

Schedule execute the task once at the specified time that depends on parameter firstInMs.

func (*GoroutineMulti) ScheduleOnInterval

func (g *GoroutineMulti) ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFunc any, params ...any) (d Disposable)

ScheduleOnInterval execute the task once at the specified time that depends on parameters both firstInMs and regularInMs.

func (*GoroutineMulti) Start

func (g *GoroutineMulti) Start()

Start the fiber work now

func (*GoroutineMulti) Stop

func (g *GoroutineMulti) Stop()

Stop the fiber work

type GoroutineSingle

type GoroutineSingle struct {
	// contains filtered or unexported fields
}

GoroutineSingle a fiber backed by a dedicated goroutine. Every job is executed by a goroutine.

func NewGoroutineSingle

func NewGoroutineSingle() *GoroutineSingle

NewGoroutineSingle create a GoroutineSingle instance

func (*GoroutineSingle) Dispose

func (g *GoroutineSingle) Dispose()

Dispose stop the fiber and release resource

func (*GoroutineSingle) Enqueue

func (g *GoroutineSingle) Enqueue(taskFunc any, params ...any)

Enqueue use the fiber to execute a task

func (*GoroutineSingle) EnqueueWithTask

func (g *GoroutineSingle) EnqueueWithTask(task Task)

EnqueueWithTask enqueue the parameter task into the queue waiting for executing.

func (*GoroutineSingle) Schedule

func (g *GoroutineSingle) Schedule(firstInMs int64, taskFunc any, params ...any) (d Disposable)

Schedule execute the task once at the specified time that depends on parameter firstInMs.

func (*GoroutineSingle) ScheduleOnInterval

func (g *GoroutineSingle) ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFunc any, params ...any) (d Disposable)

ScheduleOnInterval execute the task once at the specified time that depends on parameters both firstInMs and regularInMs.

func (*GoroutineSingle) Start

func (g *GoroutineSingle) Start()

Start the fiber work now

func (*GoroutineSingle) Stop

func (g *GoroutineSingle) Stop()

Stop the fiber work

type IScheduler

type IScheduler interface {
	Schedule(firstInMs int64, taskFunc any, params ...any) (d Disposable)
	ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFunc any, params ...any) (d Disposable)
	Enqueue(taskFunc any, params ...any)
	EnqueueWithTask(task Task)
	Remove(d Disposable)
	Dispose()
}

IScheduler an interface that for GoroutineMulti and GoroutineSingle use.

type Job

type Job struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Job store some information for cron use.

func Delay

func Delay(delayInMs int64) *Job

Delay The job executes will delay N interval.

func Every

func Every(interval int64) *Job

Every the job will execute every N everyUnit(ex atHour、atMinute、atSecond、millisecond etc..).

func EveryFriday

func EveryFriday() *Job

EveryFriday the job will execute every Friday

func EveryMonday

func EveryMonday() *Job

EveryMonday the job will execute every Monday

func EverySaturday

func EverySaturday() *Job

EverySaturday the job will execute every Saturday

func EverySunday

func EverySunday() *Job

EverySunday the job will execute every Sunday .

func EveryThursday

func EveryThursday() *Job

EveryThursday the job will execute every Thursday

func EveryTuesday

func EveryTuesday() *Job

EveryTuesday the job will execute every Tuesday

func EveryWednesday

func EveryWednesday() *Job

EveryWednesday the job will execute every Wednesday

func Everyday added in v1.0.7

func Everyday() *Job

Everyday the job will execute every day

func RightNow

func RightNow() *Job

RightNow The job executes immediately.

func (*Job) AfterExecuteTask added in v1.0.1

func (j *Job) AfterExecuteTask() *Job

AfterExecuteTask waiting for the job execute finish then calculating the job next execution time just for delay model、every N second and every N millisecond If you want some job every N minute、hour or day do once and want to calculate next execution time by after the job executed. Please use interval unit that Seconds or Milliseconds

func (*Job) At

func (j *Job) At(hh int, mm int, ss int) *Job

At the time specified at execution time

func (*Job) BeforeExecuteTask added in v1.0.1

func (j *Job) BeforeExecuteTask() *Job

BeforeExecuteTask to calculate next execution time immediately don't wait

func (*Job) Between added in v1.0.12

func (j *Job) Between(f time.Time, t time.Time) *Job

Between the job will be executed only between an assigned period (from f to f time HH:mm:ss.ff).

func (*Job) Days

func (j *Job) Days() *Job

Days a time interval of execution

func (*Job) Dispose

func (j *Job) Dispose()

Dispose Job's Dispose

func (*Job) Do

func (j *Job) Do(fun any, params ...any) Disposable

Do some job needs to execute.

func (*Job) Hours

func (j *Job) Hours() *Job

Hours a time interval of execution

func (*Job) Milliseconds added in v1.0.9

func (j *Job) Milliseconds() *Job

Milliseconds a time interval of execution

func (*Job) Minutes

func (j *Job) Minutes() *Job

Minutes a time interval of execution

func (*Job) Seconds

func (j *Job) Seconds() *Job

Seconds a time interval of execution

func (*Job) Times added in v1.0.9

func (j *Job) Times(times int64) *Job

Times set the job maximum number of executed times

type Subscriber added in v1.0.10

type Subscriber struct {
	// contains filtered or unexported fields
}

Subscriber is a struct for register to a channel

func (*Subscriber) Unsubscribe added in v1.0.10

func (c *Subscriber) Unsubscribe()

Unsubscribe remove the subscriber from the channel

type Task added in v1.0.6

type Task struct {
	// contains filtered or unexported fields
}

Task a struct

type UntilJob added in v1.0.12

type UntilJob struct {
	// contains filtered or unexported fields
}

func (*UntilJob) Do added in v1.0.12

func (u *UntilJob) Do(fun any, params ...any) Disposable

Do

type Worker added in v1.0.12

type Worker interface {
	Do(taskFunc any, params ...any) Disposable
}

func Until added in v1.0.12

func Until(time time.Time) Worker

Until

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL