Created
March 10, 2023 10:35
-
-
Save keychera/02c6a4aeb5476728bc47ea068a5719a5 to your computer and use it in GitHub Desktop.
Oven Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns oven | |
(:require [clojure.core.async :refer [>!! alts!! chan close! dropping-buffer | |
thread]])) | |
(defonce tray (atom '())) | |
(defonce oven (atom nil)) | |
(defonce orders (chan (dropping-buffer 3))) | |
(defn recipe [inp] | |
(Thread/sleep 1000) | |
(keyword (str "cake-" (str inp)))) | |
(defn stop-the-oven [] | |
(some-> @oven close!)) | |
(defn start-the-oven [] | |
(stop-the-oven) | |
(let [oven-exit-ch (chan)] | |
(thread | |
(loop [oven-turn-off oven-exit-ch] | |
(let [[order _] (alts!! [oven-turn-off orders])] | |
(if (some? order) | |
(let [{:keys [who recipe]} order] | |
(println "handling recipe from" who) | |
(swap! tray conj {:cake (recipe) | |
:by who}) | |
(Thread/sleep 1000) | |
(recur oven-turn-off)) | |
(println "oven is turning-off!"))))) | |
(reset! oven oven-exit-ch))) | |
(def i (atom 0)) | |
(defn put-recipe-in-the-oven [who] | |
(swap! i inc) | |
(let [who (str who "-" @i) | |
order {:who who | |
:recipe #(recipe @i)}] | |
(println "putting order from" who) | |
(>!! orders order))) | |
(defn whats-going-on? [] | |
#_what-to-do-here?) | |
(comment | |
(start-the-oven) | |
(stop-the-oven) | |
(put-recipe-in-the-oven "chera") | |
@tray | |
(reset! tray '()) | |
(Thread/activeCount) | |
(.. (Thread/getAllStackTraces) keySet) | |
(= (whats-going-on?) | |
{:remaining-orders [{:recipe "some" :who "chera-3"}] | |
:in-oven [{:recipe "some" :who "chera-2"}]})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment