Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/olimpias/gojob
Background task manager in golang
https://github.com/olimpias/gojob
Last synced: about 2 months ago
JSON representation
Background task manager in golang
- Host: GitHub
- URL: https://github.com/olimpias/gojob
- Owner: olimpias
- License: mit
- Created: 2016-10-16T13:49:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-05T17:15:08.000Z (almost 8 years ago)
- Last Synced: 2023-07-11T01:37:23.611Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoJob
GoJob is background task manager. It allows you to create different group of jobs and to run them in queue structure with given worker count concurrently.
## Installation
```bash
go get github.com/olimpias/gojob
```## Example
Executor interface is used for running requested tasks.
```go
type Executor interface {
Job(task * Task);
}
```In this example, Counter struct is incrementing atomicValue variable under job method. Counter struct implements Job(task * Task) method which is included by Executor interface.
```go
type Counter struct {
atomicValue uint64;
}func (counter * Counter) increment(){
atomic.AddUint64(&counter.atomicValue,1);
}// Executor interface implementation for Counter struct
func (counter * Counter) Job(task * Task){
//If you are going to use cancel operation for tasks. Don't forget to implement task.IsCancelled() check.
//Also remember to use this in loops in checking is task Cancelled.
if task.isCancelled() {
return;
}
counter.increment();
}
//Creates new Counter pointer struct
func newCounter() *Counter {
return &Counter{atomicValue:0};
}
``````go
//new Counter object
newCounterPtr := newCounter();
jobName := "incrementNumber";
var goJobManager = GoJob.SingletonJobManager();
taskName := "incrementNumber";
//Creates new job with given job name and allows 4 workers to run concurrently
goJobManager.NewJob(taskName,4);
//Add new task to job
goJobManager.AddTask(jobName,newCounterPtr); // newCounterPtr
//Start task with given jobName
goJobManager.StartTasks(jobName);```
For more details, please check Counter example under Example folder.
### Version 0.3
You will able to cancel tasks with version 0.3.
```go
taskId := goJobManager.AddTask(jobName,newCounterPtr); // will return taskID
```By using taskID, you can cancel task.
```go
goJobManager.CancelTask(taskId,jobName) // Cancels running or queued task.
//Do not forget to use @{task.isCancelled()} inside Executor interface method.!!
``````go
goJobManager.CancelAllTasks(jobName) // Cancel all running and queued tasks.
//Do not forget to use @{task.isCancelled()} inside Executor interface method.!!
```## TODO
Priority Queue Structure Implementation for GoJob
Complex Example by using GoJob