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 <https://unlicense.org>
================================================
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

```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

```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

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

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

```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

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()
}
}
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
SYMBOL INDEX (68 symbols across 15 files)
FILE: async-runner.go
function runAsync (line 5) | func runAsync(job *Job) {
FILE: dag.go
type Dag (line 4) | type Dag struct
method lastJob (line 15) | func (dag *Dag) lastJob() *Job {
method Run (line 26) | func (dag *Dag) Run() {
method RunAsync (line 35) | func (dag *Dag) RunAsync(onComplete func()) {
method Pipeline (line 48) | func (dag *Dag) Pipeline(tasks ...func()) *pipelineResult {
method Spawns (line 67) | func (dag *Dag) Spawns(tasks ...func()) *spawnsResult {
function New (line 9) | func New() *Dag {
FILE: dsl.go
type pipelineResult (line 3) | type pipelineResult struct
method Then (line 7) | func (result *pipelineResult) Then() *pipelineDSL {
method OnComplete (line 13) | func (result *pipelineResult) OnComplete(action func()) *pipelineResult {
type pipelineDSL (line 21) | type pipelineDSL struct
method Spawns (line 25) | func (dsl *pipelineDSL) Spawns(tasks ...func()) *spawnsResult {
type spawnsResult (line 32) | type spawnsResult struct
method Join (line 36) | func (result *spawnsResult) Join() *spawnsDSL {
method OnComplete (line 42) | func (result *spawnsResult) OnComplete(action func()) *spawnsResult {
type spawnsDSL (line 50) | type spawnsDSL struct
method Pipeline (line 54) | func (dsl *spawnsDSL) Pipeline(tasks ...func()) *pipelineResult {
FILE: examples/ex1/ex1.go
function main (line 5) | func main() {
function f1 (line 12) | func f1() {
function f2 (line 15) | func f2() {
function f3 (line 18) | func f3() {
FILE: examples/ex2/ex2.go
function main (line 5) | func main() {
function f1 (line 12) | func f1() {
function f2 (line 15) | func f2() {
function f3 (line 18) | func f3() {
FILE: examples/ex3/ex3.go
function main (line 5) | func main() {
function f1 (line 12) | func f1() {
function f2 (line 15) | func f2() {
function f3 (line 18) | func f3() {
function f4 (line 21) | func f4() {
FILE: examples/ex4/ex4.go
function main (line 5) | func main() {
function f1 (line 12) | func f1() {
function f2 (line 15) | func f2() {
function f3 (line 18) | func f3() {
function f4 (line 21) | func f4() {
function f5 (line 24) | func f5() {
function f6 (line 27) | func f6() {
FILE: examples/ex5/ex5.go
function main (line 5) | func main() {
function f1 (line 16) | func f1() {
function f2 (line 19) | func f2() {
function f3 (line 22) | func f3() {
function f4 (line 25) | func f4() {
function f5 (line 28) | func f5() {
function f6 (line 31) | func f6() {
function f7 (line 34) | func f7() {
function f8 (line 37) | func f8() {
FILE: examples/ex6/ex6.go
function main (line 6) | func main() {
function f1 (line 15) | func f1() {
function f2 (line 18) | func f2() {
function f3 (line 21) | func f3() {
function f4 (line 24) | func f4() {
function f5 (line 27) | func f5() {
FILE: examples/ex7/ex7.go
function main (line 7) | func main() {
function f1 (line 17) | func f1() {
function f2 (line 20) | func f2() {
function f3 (line 23) | func f3() {
function f4 (line 26) | func f4() {
FILE: examples/ex8/ex8.go
function main (line 11) | func main() {
function f1 (line 22) | func f1() {
function f2 (line 25) | func f2() {
function f3 (line 28) | func f3() {
function f4 (line 31) | func f4() {
function onComplete (line 34) | func onComplete() {
FILE: job.go
type Job (line 5) | type Job struct
FILE: pipeline/pipeline.go
function Of (line 4) | func Of(tasks ...func()) func() {
FILE: runner.go
function run (line 3) | func run(job *Job) {
FILE: sync-runner.go
function runSync (line 3) | func runSync(job *Job) {
Condensed preview — 18 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (11K chars).
[
{
"path": ".gitignore",
"chars": 192,
"preview": "# Binaries for programs and plugins\n*.exe\n*.exe~\n*.dll\n*.so\n*.dylib\n\n# Test binary, build with `go test -c`\n*.test\n\n# Ou"
},
{
"path": "LICENSE",
"chars": 1211,
"preview": "This is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, c"
},
{
"path": "README.md",
"chars": 2311,
"preview": "dag has two main concept:\n1. **Pipeline** executes the functions sequentially and in order.\n2. **Spawns** executes the f"
},
{
"path": "async-runner.go",
"chars": 264,
"preview": "package dag\n\nimport \"sync\"\n\nfunc runAsync(job *Job) {\n\n\twg := &sync.WaitGroup{}\n\twg.Add(len(job.tasks))\n\n\tfor _, task :="
},
{
"path": "dag.go",
"chars": 1236,
"preview": "package dag\n\n// Dag represents directed acyclic graph\ntype Dag struct {\n\tjobs []*Job\n}\n\n// New creates new DAG\nfunc New("
},
{
"path": "dsl.go",
"chars": 948,
"preview": "package dag\n\ntype pipelineResult struct {\n\tdag *Dag\n}\n\nfunc (result *pipelineResult) Then() *pipelineDSL {\n\treturn &pipe"
},
{
"path": "examples/ex1/ex1.go",
"chars": 205,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\n\nfunc main() {\n\n\td := dag.New()\n\td.Pipeline(f1, f2, f3)\n\td.Run()\n}\n\nfu"
},
{
"path": "examples/ex2/ex2.go",
"chars": 203,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\n\nfunc main() {\n\n\td := dag.New()\n\td.Spawns(f1, f2, f3)\n\td.Run()\n}\n\nfunc"
},
{
"path": "examples/ex3/ex3.go",
"chars": 252,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\n\nfunc main() {\n\n\td := dag.New()\n\td.Spawns(f1, f2, f3).Join().Pipeline("
},
{
"path": "examples/ex4/ex4.go",
"chars": 318,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\n\nfunc main() {\n\n\td := dag.New()\n\td.Pipeline(f1, f2, f3).Then().Spawns("
},
{
"path": "examples/ex5/ex5.go",
"chars": 410,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\n\nfunc main() {\n\n\td := dag.New()\n\td.Spawns(f1, f2, f3).\n\t\tJoin().\n\t\tPip"
},
{
"path": "examples/ex6/ex6.go",
"chars": 362,
"preview": "package main\n\nimport \"github.com/mostafa-asg/dag\"\nimport \"github.com/mostafa-asg/dag/pipeline\"\n\nfunc main() {\n\n\td := dag"
},
{
"path": "examples/ex7/ex7.go",
"chars": 304,
"preview": "package main\n\nimport (\n\t\"github.com/mostafa-asg/dag\"\n)\n\nfunc main() {\n\n\td := dag.New()\n\td.Pipeline(f1, f2).OnComplete(f3"
},
{
"path": "examples/ex8/ex8.go",
"chars": 407,
"preview": "package main\n\nimport (\n\t\"sync\"\n\n\t\"github.com/mostafa-asg/dag\"\n)\n\nvar wg = &sync.WaitGroup{}\n\nfunc main() {\n\n\twg.Add(1)\n\n"
},
{
"path": "job.go",
"chars": 199,
"preview": "package dag\n\n// Job - Each job consists of one or more tasks\n// Each Job can runs tasks in order(Sequential) or unordere"
},
{
"path": "pipeline/pipeline.go",
"chars": 162,
"preview": "package pipeline\n\n// Of wraps tasks as a single function\nfunc Of(tasks ...func()) func() {\n\n\treturn func() {\n\n\t\tfor _, t"
},
{
"path": "runner.go",
"chars": 103,
"preview": "package dag\n\nfunc run(job *Job) {\n\n\tif job.sequential {\n\t\trunSync(job)\n\t} else {\n\t\trunAsync(job)\n\t}\n\n}\n"
},
{
"path": "sync-runner.go",
"chars": 138,
"preview": "package dag\n\nfunc runSync(job *Job) {\n\n\tfor _, task := range job.tasks {\n\t\ttask()\n\t}\n\tif job.onComplete != nil {\n\t\tjob.o"
}
]
About this extraction
This page contains the full source code of the mostafa-asg/dag GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 18 files (9.0 KB), approximately 3.3k tokens, and a symbol index with 68 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.