#How elements of Directed Acyclic Graph is defined?

1 messages · Page 1 of 1 (latest)

young bear
#

Hi All!
In the docs (https://docs.dagger.io/sdk/nodejs) it is written: "When the engine receives an API request, it computes a Directed Acyclic Graph (DAG) of low-level operations required to compute the result, and starts processing operations concurrently."

My question is, how are these single "graph elements" defined? I mean, I when I define my build-and-test.mjs file, I would define it like this:

import Client, { connect } from "@dagger.io/dagger"

connect(async (client: Client) => {
  const source = client.host().directory(".", { exclude: ["node_modules/"] })

  const node = client.container().from("node:16")

  const runner = client
    .container({ id: node })
    .withMountedDirectory("/src", source)
    .withWorkdir("/src")
    .withExec(["npm", "install"])

  await runner.withExec(["npm", "test", "--", "--watchAll=false"]).exitCode()

  await runner
    .withExec(["npm", "run", "build"])
    .directory("build/")
    .export("./build")
})```

However here  I'm defining the dependency installation, testing and building *all-in-one*. Hence if I had "code-analysis" task, then I'd have to redefine "dependency installation" again, right? 
What I'm looking for instead is that for a given "task"  I define how that task is performed and what other tasks it depends on. For example, I would like to **only** define how  dependencies are installed (npm install), or how each of my other tasks (test and build)  are executed. When I define test/build, I don't want to redefine how dependency installation is to be done, but just refer to my old definition (say it's okay to build even if test fail). 
And later on, of I append my system with a new "task", say "deploy", I just want to define how deployment should happen but I don't want to redefine how dependency installation or building happens. So, I want  define "deploy" and say that it depends on "build" and the rest is calculated. I want each of my graph elements to be defined once only to avoid code repetition.

Technical Preview

unkempt oasis
#

👋 here's how you'd define the case you're thinking:

import Client, { connect } from "@dagger.io/dagger"

connect(async (client: Client) => {
    const source = client.host().directory(".", { exclude: ["node_modules/"] })

    const node = client.container().from("node:16")

    const deps = client
        .container({ id: node })
        .withMountedDirectory("/src", source)
        .withWorkdir("/src")
        .withExec(["npm", "install"])

    const test = deps.withExec(["npm", "test", "--", "--watchAll=false"]).exitCode()

    const build = deps
        .withExec(["npm", "run", "build"])

    const deploy = build.withExec(["npm", "run", "deploy"]).exitCode()


    await Promise.all([test, deploy])

}, { LogOutput: process.stderr })
#

I think you'll get idea about how the DAG works by reading the code. LMK if you still have any questions. This week we'll also be releasing some features that allows you to generate a visualization from your pipeline DAG's. Commit is already in main it just that it hasn't been released yet.

quasi nova
#

Hey @unkempt oasis , this visualization code went into the last release right? Are there any instructions on how I can use it?

unkempt oasis
#

you need the dagger-graph CLI which you need to build from main

quasi nova
#

hmm I was unable to build it from main. Is there a particular way to build it?

go build ./main.go                                                                                                  ✘ 2 main ✭ ◼
# command-line-arguments
./main.go:105:8: undefined: WrappedVertex
./main.go:133:8: undefined: WrappedVertex
unkempt oasis
#

go build ./cmd/dagger-graph from the repo root

quasi nova
#

let me try that

unkempt oasis
#

👍 that will leave a dagger-graph binary in your CWD

quasi nova
#

bingo! that worked