Repository: mostafa-asg/dag Branch: master Commit: c281549dc15e Files: 18 Total size: 9.0 KB Directory structure: gitextract_0gpyn7uj/ ├── .gitignore ├── LICENSE ├── README.md ├── async-runner.go ├── dag.go ├── dsl.go ├── examples/ │ ├── ex1/ │ │ └── ex1.go │ ├── ex2/ │ │ └── ex2.go │ ├── ex3/ │ │ └── ex3.go │ ├── ex4/ │ │ └── ex4.go │ ├── ex5/ │ │ └── ex5.go │ ├── ex6/ │ │ └── ex6.go │ ├── ex7/ │ │ └── ex7.go │ └── ex8/ │ └── ex8.go ├── job.go ├── pipeline/ │ └── pipeline.go ├── runner.go └── sync-runner.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out ================================================ FILE: LICENSE ================================================ This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to ================================================ FILE: README.md ================================================ dag has two main concept: 1. **Pipeline** executes the functions sequentially and in order. 2. **Spawns** executes the functions concurrently, so there is no ordering guarantee. ## Example 1 ![example1](images/1.png) ```Go d := dag.New() d.Pipeline(f1, f2, f3) d.Run() ``` In the above example, f1 starts first, and after completion, f2 starts then f3. Full example : [examples/ex1/ex1.go](examples/ex1/ex1.go) ## Example 2 ![example2](images/2.png) ```Go d := dag.New() d.Spawns(f1, f2, f3) d.Run() ``` The order of execution of f1, f2 and f3 is *nondeterministic* Full example : [examples/ex2/ex2.go](examples/ex2/ex2.go) ## Example 3 ![example3](images/3.png) In this example f4 must be executed after complition of f1, f2 and f3. You can use **Join** method: ```Go d := dag.New() d.Spawns(f1, f2, f3).Join().Pipeline(f4) d.Run() ``` Full example : [examples/ex3/ex3.go](examples/ex3/ex3.go) ## Example 4 ![example4](images/4.png) After *pipeline* we can use **Then** method: ```Go d := dag.New() d.Pipeline(f1, f2, f3).Then().Spawns(f4, f5, f6) d.Run() ``` Full example : [examples/ex4/ex4.go](examples/ex4/ex4.go) ## Example 5 ![example5](images/5.png) ```Go d := dag.New() d.Spawns(f1, f2, f3). Join(). Pipeline(f4, f5). Then(). Spawns(f6, f7, f8) d.Run() ``` Full example : [examples/ex5/ex5.go](examples/ex5/ex5.go) ## Example 6 ![example6](images/6.png) We want to execute two pipeline concrrently, we can use **pipeline.Of** inside the *Spawns* method: ```Go d := dag.New() d.Spawns(pipeline.Of(f1, f3), pipeline.Of(f2, f4)). Join(). Pipeline(f5) d.Run() ``` Full example : [examples/ex6/ex6.go](examples/ex6/ex6.go) ## Example 7 We can use **OnComplete** method after *Pipeline* or *Spawns* to notify when functions has completed. ```Go d := dag.New() d.Pipeline(f1, f2).OnComplete(f3). Then(). Spawns(f1, f2).OnComplete(f4) d.Run() ``` Full example : [examples/ex7/ex7.go](examples/ex7/ex7.go) ## Example 8 Basically, Run() will block until all functions are done. If you don't want to be blocked, you can use RunAsync() method. It accepts a callback function, that will be called when all functions are done. ```Go d := dag.New() d.Pipeline(f1, f2).Then().Spawns(f3, f4) d.RunAsync(onComplete) ``` Full example : [examples/ex8/ex8.go](examples/ex8/ex8.go) ================================================ FILE: async-runner.go ================================================ package dag import "sync" func runAsync(job *Job) { wg := &sync.WaitGroup{} wg.Add(len(job.tasks)) for _, task := range job.tasks { go func(task func()) { task() wg.Done() }(task) } wg.Wait() if job.onComplete != nil { job.onComplete() } } ================================================ FILE: dag.go ================================================ package dag // Dag represents directed acyclic graph type Dag struct { jobs []*Job } // New creates new DAG func New() *Dag { return &Dag{ jobs: make([]*Job, 0), } } func (dag *Dag) lastJob() *Job { jobsCount := len(dag.jobs) if jobsCount == 0 { return nil } return dag.jobs[jobsCount-1] } // Run starts the tasks // It will block until all functions are done func (dag *Dag) Run() { for _, job := range dag.jobs { run(job) } } // RunAsync executes Run on another goroutine func (dag *Dag) RunAsync(onComplete func()) { go func() { dag.Run() if onComplete != nil { onComplete() } }() } // Pipeline executes tasks sequentially func (dag *Dag) Pipeline(tasks ...func()) *pipelineResult { job := &Job{ tasks: make([]func(), len(tasks)), sequential: true, } for i, task := range tasks { job.tasks[i] = task } dag.jobs = append(dag.jobs, job) return &pipelineResult{ dag, } } // Spawns executes tasks concurrently func (dag *Dag) Spawns(tasks ...func()) *spawnsResult { job := &Job{ tasks: make([]func(), len(tasks)), sequential: false, } for i, task := range tasks { job.tasks[i] = task } dag.jobs = append(dag.jobs, job) return &spawnsResult{ dag, } } ================================================ FILE: dsl.go ================================================ package dag type pipelineResult struct { dag *Dag } func (result *pipelineResult) Then() *pipelineDSL { return &pipelineDSL{ result.dag, } } func (result *pipelineResult) OnComplete(action func()) *pipelineResult { job := result.dag.lastJob() if job != nil { job.onComplete = action } return result } type pipelineDSL struct { dag *Dag } func (dsl *pipelineDSL) Spawns(tasks ...func()) *spawnsResult { dsl.dag.Spawns(tasks...) return &spawnsResult{ dsl.dag, } } type spawnsResult struct { dag *Dag } func (result *spawnsResult) Join() *spawnsDSL { return &spawnsDSL{ result.dag, } } func (result *spawnsResult) OnComplete(action func()) *spawnsResult { job := result.dag.lastJob() if job != nil { job.onComplete = action } return result } type spawnsDSL struct { dag *Dag } func (dsl *spawnsDSL) Pipeline(tasks ...func()) *pipelineResult { dsl.dag.Pipeline(tasks...) return &pipelineResult{ dsl.dag, } } ================================================ FILE: examples/ex1/ex1.go ================================================ package main import "github.com/mostafa-asg/dag" func main() { d := dag.New() d.Pipeline(f1, f2, f3) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } ================================================ FILE: examples/ex2/ex2.go ================================================ package main import "github.com/mostafa-asg/dag" func main() { d := dag.New() d.Spawns(f1, f2, f3) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } ================================================ FILE: examples/ex3/ex3.go ================================================ package main import "github.com/mostafa-asg/dag" func main() { d := dag.New() d.Spawns(f1, f2, f3).Join().Pipeline(f4) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } func f4() { println("f4") } ================================================ FILE: examples/ex4/ex4.go ================================================ package main import "github.com/mostafa-asg/dag" func main() { d := dag.New() d.Pipeline(f1, f2, f3).Then().Spawns(f4, f5, f6) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } func f4() { println("f4") } func f5() { println("f5") } func f6() { println("f6") } ================================================ FILE: examples/ex5/ex5.go ================================================ package main import "github.com/mostafa-asg/dag" func main() { d := dag.New() d.Spawns(f1, f2, f3). Join(). Pipeline(f4, f5). Then(). Spawns(f6, f7, f8) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } func f4() { println("f4") } func f5() { println("f5") } func f6() { println("f6") } func f7() { println("f7") } func f8() { println("f8") } ================================================ FILE: examples/ex6/ex6.go ================================================ package main import "github.com/mostafa-asg/dag" import "github.com/mostafa-asg/dag/pipeline" func main() { d := dag.New() d.Spawns(pipeline.Of(f1, f3), pipeline.Of(f2, f4)). Join(). Pipeline(f5) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } func f4() { println("f4") } func f5() { println("f5") } ================================================ FILE: examples/ex7/ex7.go ================================================ package main import ( "github.com/mostafa-asg/dag" ) func main() { d := dag.New() d.Pipeline(f1, f2).OnComplete(f3). Then(). Spawns(f1, f2).OnComplete(f4) d.Run() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("complete") } func f4() { println("finish") } ================================================ FILE: examples/ex8/ex8.go ================================================ package main import ( "sync" "github.com/mostafa-asg/dag" ) var wg = &sync.WaitGroup{} func main() { wg.Add(1) d := dag.New() d.Pipeline(f1, f2).Then().Spawns(f3, f4) d.RunAsync(onComplete) wg.Wait() } func f1() { println("f1") } func f2() { println("f2") } func f3() { println("f3") } func f4() { println("f4") } func onComplete() { println("All functions have completed") wg.Done() } ================================================ FILE: job.go ================================================ package dag // Job - Each job consists of one or more tasks // Each Job can runs tasks in order(Sequential) or unordered type Job struct { tasks []func() sequential bool onComplete func() } ================================================ FILE: pipeline/pipeline.go ================================================ package pipeline // Of wraps tasks as a single function func Of(tasks ...func()) func() { return func() { for _, task := range tasks { task() } } } ================================================ FILE: runner.go ================================================ package dag func run(job *Job) { if job.sequential { runSync(job) } else { runAsync(job) } } ================================================ FILE: sync-runner.go ================================================ package dag func runSync(job *Job) { for _, task := range job.tasks { task() } if job.onComplete != nil { job.onComplete() } }