Skip to content

Instantly share code, notes, and snippets.

@keychera
Created March 10, 2023 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keychera/02c6a4aeb5476728bc47ea068a5719a5 to your computer and use it in GitHub Desktop.
Save keychera/02c6a4aeb5476728bc47ea068a5719a5 to your computer and use it in GitHub Desktop.
Oven Queue
(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