Categorygithub.com/architagr/taskscheduler
modulepackage
0.0.0-20201113172345-e44932d3b5a2
Repository: https://github.com/architagr/taskscheduler.git
Documentation: pkg.go.dev

# README

taskScheduler

This library can be used at places where you want to create tasks that have to be executed after some particular time duration.

To use this you have to 1st call the AddChannel function sending in the channel name and a function that have a read only channel as a input (to listen to this channel).

Then call a AddTask function with channel name in which channel to add the task, duration in which you want the event and data that you want to receive once the duration ends.

for example if we are configuring this in a project then -

In main.go I have created a function configureChannel() that is run as a goroutine in the main function

func configureChannel() {
	taskscheduler.AddChannel("channel1", receiveDataChannel1)
	ok, ID, err := taskscheduler.AddTask("channel1", time.Second*time.Duration(10), "data1")
	if !ok {
		fmt.Println("error adding task:",err)
	} else {
		fmt.Println("no error adding task", ID)
	}
}

func receiveDataChannel1(ch <-chan ts.ChannelData) {
	for channelTask := range ch {
		fmt.Println("received data:",channelTask.Data,"for task id :", channelTask.ID)
	}
}

func main(){
    fmt.Println("run")
    go configureChannel()
    time.Sleep(time.Second*time.Duration(60))
}

# Functions

AddChannel Adding a new channel to the list of all channels, the output indicates if the channels have been added by the name provided as an input, also the input function is used to send the reference of the channel to the function in read only mode so that the function can implement logic to receive data we have a limit of 10 message in the channel.
AddTask adds a task in the channel, once the task is complete (duration has passed) the data that is been passed in this will be send over the channel the data along with the channel name and task ID is been send to the function that was passed when creating a channel.
RemoveChannel removes a channel that exixts in the list of all channels, the output indicates if the channels have been removed by the name provided as an input and if all task in it have been stoped.
RemoveTask this stops a task by the task id.

# Structs

ChannelData channel data emited by the channel once the task has ended.
Task this struct has data for each task, task can be uniquely identified by the ID field.