#Start multiple services from one function and expose to the host

1 messages ยท Page 1 of 1 (latest)

fallen granite
#

I am using a database and cache in my application, and I need these services up and running when I execute my tests; the database has a UI which I use check the data.
Right now, some of the tests are failing times, I need to inspect the DB using UI.
I cannot find a way to expose the DB service to the host while running the tests.

Would prefer:

  • dagger call test
    • add terminal() before the exec command
    • run the test one by one in python container and inspect the database when the test fails.

Would be 100% okay with:

  • running both the services using one function
    • dagger call run-services
  • running the python container, passing the services as host service, run the test one by one.

@object_type
class FooTests:
@function
def run_db(self) -> dagger.Service:
return (
dag.container()
.from_("arangodb:3.12.3")
.with_env_variable(name="ARANGO_ROOT_PASSWORD", value="mycomplexpass")
.with_exposed_port(8529)
.as_service())

@function
def run_cache(self) -> dagger.Service:
    return (dag.container()
            .from_("valkey/valkey:8.0.2")
            .with_exposed_port(6379)
            .as_service())
def test(self, 
        source: Annotated[dagger.Directory, DefaultPath("/")]
    ) -> str:
    db = self.run_db()
    cache = self.run_cache()
    return (dag.container()
        .from_("python:3.12-slim-bullseye")
        .with_service_binding("db", db)
        .with_service_binding("cache", cache)
        .with_mounted_directory("/mycode", source)
        .with_exec(["sh", "-c", "pytest"], use_entrypoint=False)
        .stdout)

@function
async def run_services(self) -> str:
    await self.run_db().up() ## this doesn't work -- not sure how to call it.
    await self.run_cache().up() ## this doesn't work -- not sure how to call it.
    return ""
fallen granite
#

great!!
thank you for your speedy response.

I will have a look ๐Ÿ™‚

split dirge
#

๐Ÿš€

#

np. Let us know if you have further questions

fallen granite
#

@split dirge I noticed that when I do dagger call my-service up, the process doesn't go into background.
Meaning, the terminal I run dagger rom stays occupied and if i type in q, the service terminates and i get access back

split dirge