Skip to content

Commit

Permalink
Merge branch 'release-0.1.0' (early part)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 9, 2013
2 parents b178cf5 + 8c88c73 commit d03eed1
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 84 deletions.
102 changes: 53 additions & 49 deletions pom.xml
@@ -1,49 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>task</artifactId>
<version>0.0.3-SNAPSHOT</version>
<name>Task</name>
<packaging>jar</packaging>
<description>Clojure library for running and managing interactive tasks</description>

<parent>
<groupId>net.mikera</groupId>
<artifactId>clojure-pom</artifactId>
<version>0.0.4</version>
</parent>

<scm>
<connection>scm:git:git@github.com:mikera/${project.artifactId}.git</connection>
<url>scm:git:git@github.com:mikera/${project.artifactId}.git</url>
<developerConnection>scm:git:git@github.com:mikera/${project.artifactId}.git</developerConnection>
<tag>HEAD</tag>
</scm>

<repositories>
<repository>
<id>clojars.org</id>
<name>Clojars repository</name>
<url>https://clojars.org/repo</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>clj-time</groupId>
<artifactId>clj-time</artifactId>
<version>0.4.4</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>tools.macro</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>net.mikera</groupId>
<artifactId>cljunit</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>task</artifactId>
<version>0.1.0</version>
<name>Task</name>
<packaging>jar</packaging>
<description>Clojure library for running and managing interactive tasks</description>

<parent>
<groupId>net.mikera</groupId>
<artifactId>clojure-pom</artifactId>
<version>0.0.4</version>
</parent>

<scm>
<connection>scm:git:git@github.com:mikera/${project.artifactId}.git</connection>
<url>scm:git:git@github.com:mikera/${project.artifactId}.git</url>
<developerConnection>scm:git:git@github.com:mikera/${project.artifactId}.git</developerConnection>
<tag>task-0.1.0</tag>
</scm>

<repositories>
<repository>
<id>clojars.org</id>
<name>Clojars repository</name>
<url>https://clojars.org/repo</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>clj-time</groupId>
<artifactId>clj-time</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>tools.macro</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>net.mikera</groupId>
<artifactId>cljunit</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.mikera</groupId>
<artifactId>clojure-utils</artifactId>
<version>0.2.1</version>
</dependency>
</dependencies>
</project>
97 changes: 62 additions & 35 deletions src/main/clojure/task/core.clj
@@ -1,5 +1,6 @@
(ns task.core
(:require [clj-time.core :as time])
(:require [mikera.cljutils.text :as text])
(:require [clojure.pprint])
(:require [clojure.repl]))

Expand All @@ -9,7 +10,7 @@
(defrecord TaskData [])

(defn task*
"Creates a map representing the specified function as a task"
"Creates a task data structure representing the specified function as a task"
([options function]
(TaskData. nil
(merge options
Expand All @@ -30,38 +31,56 @@

;; =====================================================================================
;; Task query functions

(defn get-task
"Returns the current task associated with the given ID.
If called with an old task instance, returns the latest task for the same ID"
[task-or-id]
(let [id (cond
(associative? task-or-id) (:id task-or-id)
:else task-or-id)
task (@tasks id)]
task))

(defn stopped? [task]
(or
(nil? task)
(#{:stopped} (:status task))))
(let [task (get-task task)]
(or
(nil? task)
(#{:stopped} (:status task)))))


(defn complete? [task]
(or
(nil? task)
(#{:complete :error :stopped} (:status task))))
(let [task (get-task task)]
(or
(nil? task)
(#{:complete :error :stopped} (:status task)))))

(defn running? [task]
(let [task (get-task task)]
(and task
(#{:started} (:status task)))))

(defn get-task
"Returns the current task associated with the given ID.

If called with an old task instance, returns the latest task for the same ID"
[task-or-id]
(let [id (cond
(associative? task-or-id) (:id task-or-id)
:else task-or-id)
task (@tasks id)]
task))
(def task-fields
{:id {:length 6}
:status {:length 10}
:source {:length 20}
:options {:length 20}
:result {:length 15}})

(def task-summary-fields [:id :status :source :options :result])

(defn task-summary [task]
{:id (:id task)
:status (:status task)
:source (:source task)
:options (:options task)
:result (:result task)})
(defn task-summary [task & {:keys [fields]
:or {fields task-summary-fields}}]
(reduce
(fn [m k]
(assoc m k
(let [len (:length (task-fields k))
s (str (k task))]
(text/pad-right (text/truncate-dotted s len) len))))
{}
fields))


;; =====================================================================================
Expand Down Expand Up @@ -131,7 +150,7 @@
If set to true, all results will be saved in the vector :results in the task.
:timeout
A number of milliseconds to run the task for. If the tomeout is reaced during
A number of milliseconds to run the task for. If the timeout is reached during
execution of the task, it will be allowed to complete.
:sleep
Expand Down Expand Up @@ -163,12 +182,15 @@


(defn ps
([]
(ps (vals @tasks)))
([tasks]
(ps task-summary-fields tasks))
([ks tasks]
(clojure.pprint/print-table ks (map task-summary tasks))))
"Prints a table of tasks"
([& {:keys [filter fields tasks sort]
:or {fields task-summary-fields
filter (constantly true)
sort :id
tasks (vals @tasks)}}]
(let [tasks (clojure.core/filter filter tasks)
tasks (if sort (sort-by sort tasks) tasks)]
(clojure.pprint/print-table fields (map task-summary tasks)))))

(defn stop [task]
(dosync
Expand Down Expand Up @@ -213,11 +235,13 @@
;; ==================================================================================
;; Task execution

(defn finish-task [task]
(let [task (assoc task :finish-time (time/now))
promise (:promise task)]
(if promise (deliver promise (:result task)))
task))
(defn- finish-task
([task]
(let [task (get-task task)
task (assoc task :finish-time (time/now))
promise (:promise task)]
(if promise (deliver promise (:result task)))
task)))

(defn task-loop [task]
(if (complete? task)
Expand Down Expand Up @@ -260,6 +284,9 @@
`(run-task (task ~code)))

([options code]
`(run-task (task ~options ~code))))
`(run-task (task ~options ~code)))

([code k1 v1 & more]
`(run-task (task {~k1 ~@(cons v1 more)} ~code))))


0 comments on commit d03eed1

Please sign in to comment.