-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update state machine definitions, type hints for new aws sdk builder …
…class, simplify reloaded, update readme, release 0.5.8
- Loading branch information
Showing
7 changed files
with
84 additions
and
170 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,42 @@ | ||
(ns stepwise.reloaded | ||
(:require [stepwise.client :as client] | ||
[stepwise.arns :as arns] | ||
[stepwise.model :as mdl] | ||
[stepwise.core :as core] | ||
[clojure.string :as strs] | ||
[stepwise.activities :as activities] | ||
[clojure.set :as sets] | ||
[stepwise.sugar :as sgr] | ||
[stepwise.iam :as iam] | ||
[stepwise.workers :as workers]) | ||
(:import (com.amazonaws.services.stepfunctions.model StateMachineDeletingException | ||
StateMachineDoesNotExistException | ||
ExecutionDoesNotExistException) | ||
(java.util UUID))) | ||
|
||
(def max-cycles-per-minute 120) | ||
(def version-delimiter "_SNAPSHOT") | ||
(def version-delimiter-re (re-pattern version-delimiter)) | ||
|
||
(defn get-next-version [current-arns] | ||
(let [arn (-> current-arns sort reverse first) | ||
last-ver (some-> arn | ||
(strs/split version-delimiter-re) | ||
second | ||
(Integer/parseInt))] | ||
(if last-ver | ||
(if (>= last-ver max-cycles-per-minute) | ||
0 | ||
(+ last-ver 1)) | ||
0))) | ||
|
||
(def version-format | ||
(str "%0" (count (str max-cycles-per-minute)) "d")) | ||
|
||
(defn deversion-name [nm] | ||
(when (re-find version-delimiter-re nm) | ||
(first (strs/split (name nm) | ||
version-delimiter-re)))) | ||
|
||
(defn version-name [nm version] | ||
(keyword (namespace nm) | ||
(str (name nm) | ||
version-delimiter | ||
(format version-format version)))) | ||
|
||
(defn purge-machines [arns] | ||
(doseq [arn arns] | ||
(doseq [execution (try (::mdl/executions (client/list-executions arn)) | ||
(catch StateMachineDoesNotExistException _))] | ||
(try (client/stop-execution (::mdl/arn execution)) | ||
(catch ExecutionDoesNotExistException _))) | ||
(try (client/delete-state-machine arn) | ||
(catch StateMachineDeletingException _)))) | ||
|
||
(defn purge-activities [arns] | ||
(doseq [arn arns] | ||
(client/delete-activity arn))) | ||
|
||
(defn get-family-arns [machine-name] | ||
(into #{} | ||
(comp (filter #(= (deversion-name (::mdl/name %)) | ||
(arns/make-name machine-name))) | ||
(map ::mdl/arn)) | ||
(::mdl/state-machines (client/list-state-machines)))) | ||
[stepwise.arns :as arns]) | ||
(:import (java.util UUID))) | ||
|
||
(defn machine-name->snapshot [machine-name] | ||
(str (when-let [mns (namespace machine-name)] | ||
(str mns "-")) | ||
(name machine-name) | ||
"-" | ||
(str (System/currentTimeMillis)))) | ||
|
||
(defn activity-name->snapshot [snapshot-name activity-names] | ||
(into {} | ||
(map (fn [activity-name] | ||
[activity-name (keyword (str snapshot-name | ||
(when-let [ans (namespace activity-name)] | ||
(str "-" ans))) | ||
(name activity-name))])) | ||
activity-names)) | ||
|
||
(defn run-execution [machine-name definition task-handlers input] | ||
(let [machine-arns (get-family-arns machine-name) | ||
version (get-next-version machine-arns) | ||
machine-name (version-name machine-name version) | ||
activity-names (activities/get-names definition) | ||
member-activity? (into #{} (map arns/make-name) activity-names) | ||
; TODO paginate | ||
activity-arns (into #{} | ||
(comp (filter #(member-activity? (deversion-name (::mdl/name %)))) | ||
(map ::mdl/arn)) | ||
(::mdl/activities (client/list-activities)))] | ||
|
||
(purge-machines machine-arns) | ||
(purge-activities activity-arns) | ||
|
||
(let [activity->snapshot (into {} | ||
(map #(vector % (version-name % version))) | ||
activity-names) | ||
_ (core/create-state-machine machine-name | ||
(activities/resolve-kw-resources activity->snapshot | ||
definition)) | ||
workers (core/start-workers (sets/rename-keys task-handlers | ||
activity->snapshot)) | ||
result (core/run-execution machine-name | ||
{:input input | ||
:execution-name (str (UUID/randomUUID))})] | ||
(core/kill-workers workers) | ||
result))) | ||
|
||
; TODO clean-up function to purge all snapshot machines/activities for a machine | ||
(let [snapshot-name (machine-name->snapshot machine-name) | ||
activity-names (activities/get-names definition) | ||
activity->snapshot (activity-name->snapshot snapshot-name activity-names) | ||
definition (activities/resolve-kw-resources activity->snapshot | ||
definition) | ||
snapshot-arn (core/ensure-state-machine snapshot-name definition) | ||
workers (core/start-workers (sets/rename-keys task-handlers | ||
activity->snapshot)) | ||
result (core/run-execution snapshot-name | ||
{:input input | ||
:execution-name (str (UUID/randomUUID))})] | ||
(core/kill-workers workers) | ||
(doseq [[_ snapshot-name] activity->snapshot] | ||
(client/delete-activity (arns/get-activity-arn snapshot-name))) | ||
(client/delete-state-machine snapshot-arn) | ||
result)) | ||
|
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