alda-lang / alda-server-clj

A Clojure implementation of an Alda server
Other
2 stars 2 forks source link

Make worker status responses threadsafe #8

Closed daveyarwood closed 7 years ago

daveyarwood commented 7 years ago

I created a brittle "worker status" system based on mutable state and relying on the client to make requests in a certain way.

Now that we're working on a new client-side REPL (https://github.com/alda-lang/alda-client-java/issues/1), bugs with this approach are starting to surface. It's becoming obvious that I need to re-think how this system works and make it more reliable.

How it works now

Problem

I think the problem with this is that when playing tiny scores in rapid succession (e.g. evaluating lines of code in the Alda REPL), status requests can come from the client in an arbitrary order, and potentially overlap with status requests for other lines of code (scores) submitted to be played. The worker can only really be in the state of handling one score at a time, so it's possible to send the wrong data sometimes.

Solution

Associate each parse/play job with a unique ID, and store a number (say, 10) of previous results (state, parsed score and/or error) in a map keyed by the job ID. Make these results available to the client at any time regardless of whether a new job has started; the client just needs to provide the job ID to get the status of that score being parsed/played.

To make this backwards compatible, we can default to the most recent job, if no job ID is provided. This is the current behavior.

An added benefit is that this sets us up for clients to be able to pause and stop the playback of individual jobs ( https://github.com/alda-lang/alda-sound-engine-clj/issues/3 ).

I think a FIFO cache from clojure/core.cache might work for the map of job ID => status. We can set the threshold at 10 (or whatever) and the cache will only hold up to that many results and discard the old ones.

daveyarwood commented 7 years ago

Fixed in 0.2.0.