Revision e9aaa3c6 htools/Ganeti/Jobs.hs
b/htools/Ganeti/Jobs.hs | ||
---|---|---|
1 |
{-# LANGUAGE TemplateHaskell #-} |
|
2 |
|
|
1 | 3 |
{-| Implementation of the job information. |
2 | 4 |
|
3 | 5 |
-} |
... | ... | |
32 | 34 |
import qualified Text.JSON as J |
33 | 35 |
|
34 | 36 |
import qualified Ganeti.Constants as C |
37 |
import qualified Ganeti.THH as THH |
|
35 | 38 |
|
36 | 39 |
-- | Our ADT for the OpCode status at runtime (while in a job). |
37 |
data OpStatus = OP_STATUS_QUEUED |
|
38 |
| OP_STATUS_WAITING |
|
39 |
| OP_STATUS_CANCELING |
|
40 |
| OP_STATUS_RUNNING |
|
41 |
| OP_STATUS_CANCELED |
|
42 |
| OP_STATUS_SUCCESS |
|
43 |
| OP_STATUS_ERROR |
|
44 |
deriving (Eq, Enum, Bounded, Show, Read) |
|
45 |
|
|
46 |
instance JSON OpStatus where |
|
47 |
showJSON os = showJSON w |
|
48 |
where w = case os of |
|
49 |
OP_STATUS_QUEUED -> C.opStatusQueued |
|
50 |
OP_STATUS_WAITING -> C.opStatusWaiting |
|
51 |
OP_STATUS_CANCELING -> C.opStatusCanceling |
|
52 |
OP_STATUS_RUNNING -> C.opStatusRunning |
|
53 |
OP_STATUS_CANCELED -> C.opStatusCanceled |
|
54 |
OP_STATUS_SUCCESS -> C.opStatusSuccess |
|
55 |
OP_STATUS_ERROR -> C.opStatusError |
|
56 |
readJSON s = case readJSON s of |
|
57 |
J.Ok v | v == C.opStatusQueued -> J.Ok OP_STATUS_QUEUED |
|
58 |
| v == C.opStatusWaiting -> J.Ok OP_STATUS_WAITING |
|
59 |
| v == C.opStatusCanceling -> J.Ok OP_STATUS_CANCELING |
|
60 |
| v == C.opStatusRunning -> J.Ok OP_STATUS_RUNNING |
|
61 |
| v == C.opStatusCanceled -> J.Ok OP_STATUS_CANCELED |
|
62 |
| v == C.opStatusSuccess -> J.Ok OP_STATUS_SUCCESS |
|
63 |
| v == C.opStatusError -> J.Ok OP_STATUS_ERROR |
|
64 |
| otherwise -> J.Error ("Unknown opcode status " ++ v) |
|
65 |
_ -> J.Error ("Cannot parse opcode status " ++ show s) |
|
40 |
$(THH.declareSADT "OpStatus" |
|
41 |
[ ("OP_STATUS_QUEUED", 'C.opStatusQueued) |
|
42 |
, ("OP_STATUS_WAITING", 'C.opStatusWaiting) |
|
43 |
, ("OP_STATUS_CANCELING", 'C.opStatusCanceling) |
|
44 |
, ("OP_STATUS_RUNNING", 'C.opStatusRunning) |
|
45 |
, ("OP_STATUS_CANCELED", 'C.opStatusCanceled) |
|
46 |
, ("OP_STATUS_SUCCESS", 'C.opStatusSuccess) |
|
47 |
, ("OP_STATUS_ERROR", 'C.opStatusError) |
|
48 |
]) |
|
49 |
$(THH.makeJSONInstance ''OpStatus) |
|
66 | 50 |
|
67 | 51 |
-- | The JobStatus data type. Note that this is ordered especially |
68 | 52 |
-- such that greater\/lesser comparison on values of this type makes |
69 | 53 |
-- sense. |
70 |
data JobStatus = JOB_STATUS_QUEUED |
|
71 |
| JOB_STATUS_WAITING |
|
72 |
| JOB_STATUS_RUNNING |
|
73 |
| JOB_STATUS_SUCCESS |
|
74 |
| JOB_STATUS_CANCELING |
|
75 |
| JOB_STATUS_CANCELED |
|
76 |
| JOB_STATUS_ERROR |
|
77 |
deriving (Eq, Enum, Ord, Bounded, Show, Read) |
|
78 |
|
|
79 |
instance JSON JobStatus where |
|
80 |
showJSON js = showJSON w |
|
81 |
where w = case js of |
|
82 |
JOB_STATUS_QUEUED -> C.jobStatusQueued |
|
83 |
JOB_STATUS_WAITING -> C.jobStatusWaiting |
|
84 |
JOB_STATUS_CANCELING -> C.jobStatusCanceling |
|
85 |
JOB_STATUS_RUNNING -> C.jobStatusRunning |
|
86 |
JOB_STATUS_CANCELED -> C.jobStatusCanceled |
|
87 |
JOB_STATUS_SUCCESS -> C.jobStatusSuccess |
|
88 |
JOB_STATUS_ERROR -> C.jobStatusError |
|
89 |
readJSON s = case readJSON s of |
|
90 |
J.Ok v | v == C.jobStatusQueued -> J.Ok JOB_STATUS_QUEUED |
|
91 |
| v == C.jobStatusWaiting -> J.Ok JOB_STATUS_WAITING |
|
92 |
| v == C.jobStatusCanceling -> J.Ok JOB_STATUS_CANCELING |
|
93 |
| v == C.jobStatusRunning -> J.Ok JOB_STATUS_RUNNING |
|
94 |
| v == C.jobStatusSuccess -> J.Ok JOB_STATUS_SUCCESS |
|
95 |
| v == C.jobStatusCanceled -> J.Ok JOB_STATUS_CANCELED |
|
96 |
| v == C.jobStatusError -> J.Ok JOB_STATUS_ERROR |
|
97 |
| otherwise -> J.Error ("Unknown job status " ++ v) |
|
98 |
_ -> J.Error ("Unknown job status " ++ show s) |
|
54 |
$(THH.declareSADT "JobStatus" |
|
55 |
[ ("JOB_STATUS_QUEUED", 'C.jobStatusQueued) |
|
56 |
, ("JOB_STATUS_WAITING", 'C.jobStatusWaiting) |
|
57 |
, ("JOB_STATUS_CANCELING", 'C.jobStatusCanceling) |
|
58 |
, ("JOB_STATUS_RUNNING", 'C.jobStatusRunning) |
|
59 |
, ("JOB_STATUS_CANCELED", 'C.jobStatusCanceled) |
|
60 |
, ("JOB_STATUS_SUCCESS", 'C.jobStatusSuccess) |
|
61 |
, ("JOB_STATUS_ERROR", 'C.jobStatusError) |
|
62 |
]) |
|
63 |
$(THH.makeJSONInstance ''JobStatus) |
Also available in: Unified diff