duct-framework / duct

Server-side application framework for Clojure
MIT License
1.13k stars 51 forks source link

Top components #74

Closed LukasRychtecky closed 5 years ago

LukasRychtecky commented 6 years ago

Hi,

I've found out that when I define a top component (e.g. a scheduler for a periodic task - :myapp/scheduler) and this component is not dependency for any other component, the component is not initialized. When I put it into keys sequence when starting the app, it worked as expected. E.g.:

(defn -main
  [& args]
  (let [keys (or (duct/parse-keys args) [:duct/daemon :myapp/scheduler])]
    (-> (duct/read-config (io/resource "myapp/config.edn"))
        (duct/prep keys)
        (duct/exec keys))))

Am I right that all top components have to be explicitly passed into keys when prep and exec?

zerg000000 commented 6 years ago

you can ask this question in https://clojurians.slack.com/messages/C5K1SHR6X

short answer. At current version of duct, it will build the dependency tree for :duct/daemon

if you want duct to initialize your component, derive your key from :duct/daemon.

(derive :myapp/scheduler :duct/server)

or define it in duct_hierarchy.edn under your resources root

{:myapp/scheduler [:duct/server]}

Reference

https://github.com/duct-framework/core/blob/3964492d8d4be266ba7614d29263488e03482112/src/duct/core.clj#L162 https://github.com/duct-framework/core/blob/3964492d8d4be266ba7614d29263488e03482112/src/duct/core.clj#L183

weavejester commented 6 years ago

@zerg000000 is correct. To be more precise, when running Duct from -main, only keys that are derived from :duct/daemon are executed. This is because when running in a production environment, there are keys that you may not wish to run on every server (such as migrators, compilers, etc.).

When starting Duct through the REPL and (go), all keys are initiated by default.

This is detailed briefly in the duct/core docs, but it really needs better documentation.

LukasRychtecky commented 6 years ago

Thanks for answering. I read the duct/docs, but it wasn't obvious for me (to derive or use duct_hierarchy.edn). @weavejester what do you think about I'd create a PR withing a note about think into duct/core's README.md?

weavejester commented 6 years ago

I wonder if duct/core is the right place for it. Is that where you looked first of all? Or would the Duct wiki on this project be a better place?

LukasRychtecky commented 6 years ago

First I looked into Duct's Wiki https://github.com/duct-framework/duct/wiki/Configuration and after I went to https://github.com/duct-framework/core README.

LukasRychtecky commented 6 years ago

@weavejester What do you think? Where to put the notes?

weavejester commented 6 years ago

The wiki is probably the best idea - it's easy to edit, and it was the first place you looked.

LukasRychtecky commented 6 years ago

I've updated the Wiki https://github.com/duct-framework/duct/wiki/Configuration#top-level-components

If it's OK I think the issue can be closed.