-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.sh
executable file
·325 lines (279 loc) · 8.22 KB
/
play.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash -e
# @installable
MYSELF="$(readlink -f "$0")"
MYDIR="${MYSELF%/*}"
ME=$(basename $MYSELF)
source $MYDIR/env
[[ -f $LOCAL_ENV ]] && source $LOCAL_ENV
source $MYDIR/log.sh
source $MYDIR/db.sh
# @return task if created
function new_task() {
name="$1"
project_id="$2"
old_task=$($MYDIR/psql.sh "
select * from tasks
where name = '$name'
and project_id = $project_id
")
if [[ -z "$old_task" ]]; then
repo=$(repo_root_quiet)
if [[ -d "$repo" ]]; then
info "'$name' task repo defined as $repo (<enter> to confirm):"
read confirmation
else
info "define a repo for '$name' [empty]:"
read repo
while [[ -n "$repo" && ! -d "$repo" ]]; do
err "not a repo: $repo - try again:"
read repo
done
fi
new_task=$($MYDIR/psql.sh "
INSERT INTO tasks
(name, project_id, external_id, repo)
SELECT '$name', $project_id, '$external_id', '$repo'
RETURNING *
")
info "new task created"
echo "$new_task"
else
info "using old task"
echo "$old_task"
fi
if [[ -n "$external_id" ]]; then
info "updating external id of '$name' to '$external_id'"
>&2 $MYDIR/psql.sh "
update tasks set external_id = '$external_id'
where name = '$name' and project_id = $project_id
"
else
issue_id=$(echo $name | cut -d'-' -f2)
if [[ $(nan $issue_id) != true ]]; then
info "updating external id of '$name' to '$issue_id'"
>&2 $MYDIR/psql.sh "
update tasks set external_id = '$issue_id'
where name = '$name' and project_id = $project_id
"
fi
fi
}
# finishes unclosed executions
function check_open_executions() {
chktid=$1
debug "checking open executions for $chktid ..."
ex=false
while read open_execution
do
tid=$(echo $open_execution | cut -d'=' -f1)
tname=$(echo $open_execution | cut -d'=' -f2)
info "you were previously working on '$tname', ending open executions for task #$tid ..."
$MYDIR/spend.sh "$tid"
comment $tid "started work on task #$chktid"
pause $tid
ex=true
done < <($MYDIR/psql-map.sh \
"executions e join tasks t on t.id=e.task_id" \
"t.id,t.name" \
"task_id <> $chktid and e.finish is null group by t.id")
debug "open executions checked for $chktid"
if [[ $ex == true ]]; then
debug "...and found"
fi
}
# FIXME not pausing when open execution exists
# @return execution if created
function play() {
pltask_id="$1"
if [[ -z "$pltask_id" ]]; then
err "task id cannot be null"
exit 1
fi
info "playing #$pltask_id ..."
check_open_executions $pltask_id
debug "creating execution if not exists for $pltask_id ..."
$MYDIR/psql.sh "WITH open_execution AS (
select * from executions where task_id = $pltask_id AND finish is null
) INSERT INTO executions
(task_id, start)
SELECT $pltask_id, now()
WHERE NOT EXISTS (SELECT * FROM open_execution)
RETURNING *
;"
}
# @return updated task
function pause() {
ptask_id="$1"
if [[ ! -n "$ptask_id" ]]; then
err "task id cannot be null"
exit 1
fi
debug "#$ptask_id - starting pause process..."
check_open_executions $ptask_id
$MYDIR/psql.sh "WITH execution AS (
UPDATE executions
SET finish = now(), elapsed = (now() - start)
WHERE task_id = $ptask_id AND finish is null
RETURNING *
) update tasks set elapsed = elapsed + (select elapsed from execution)
where id = $ptask_id
returning *
;"
debug "#$ptask_id - finished pause process"
}
# @return last comments
function comment() {
ctask_id="$1"
content="$2"
debug "// $content"
$MYDIR/psql.sh "insert into comments (task_id, content)
select $ctask_id, '$content'
returning *
;"
}
# @return tasks that match name
function find() {
task_id="$1"
content="$2"
$MYDIR/psql.sh "insert into comments (task_id, content)
select $task_id, '$content'
returning *
;"
}
name="$1"
if [[ -n "$1" && "$1" != -* ]]; then
shift
fi
project_id=1
external_id=''
new=false
pausing=false
while test $# -gt 0
do
case "$1" in
--list|-l)
latest_tasks
exit 0
;;
--status|-s)
$MYDIR/psql.sh "
select
t.name, e.start, e.finish, (coalesce(e.finish, now())-e.start) elapsed
from executions e
join tasks t on t.id=e.task_id
order by e.id desc
limit 1
;" --full
exit 0
;;
--remove-last|-r)
info "deleting last task..."
$MYDIR/psql.sh "delete from tasks where id = (select id from tasks order by id desc limit 1) returning *"
exit 0
;;
--connect|-c)
info "connecting to local db..."
psql -U $DB_USER $DB_NAME
exit 0
;;
--find|-f)
shift
search="$1"
info "finding last 5 tasks like '$search' ..."
$MYDIR/psql.sh "select * from tasks where name ilike '%$search%' order by id desc limit 5;" --full
exit 0
;;
--project|-p)
shift
project_id="${1}"
;;
--external-id|--ex)
shift
external_id=$1
;;
--pause)
# defining as null name to go to pause flow
name=''
pausing=true
;;
-*)
echo "bad option '$1'"
exit 1
;;
esac
shift
done
if [[ -n "$name" ]]; then
# switching to a specific task
debug "will work with task '$name' on project '$project_id' ..."
task=$(new_task "$name" $project_id)
if [[ -n "$task" ]]; then
task_id=$(echo "$task" | cut -d'|' -f1)
info "using task #$task_id '$name'"
new=true
else
debug "previously open tasks for '$name' found, looking for latest task execution ..."
task=$($MYDIR/psql.sh "select
t.id,e.finish
from tasks t join executions e on e.task_id=t.id
where t.name = '$name'
order by e.id desc
limit 1
")
task_id=$(echo $task | cut -d'|' -f1)
finish=$(echo $task | cut -d'|' -f2)
fi
else
# determine current task
task=$($MYDIR/psql.sh "select t.id,e.finish,t.name from tasks t join executions e on e.task_id=t.id order by e.id desc limit 1")
task_id=$(echo $task | cut -d'|' -f1)
finish=$(echo $task | cut -d'|' -f2)
name=$(echo $task | cut -d'|' -f3)
fi
if [[ -n "$finish" || $new == true ]]; then
debug "finish: '$finish', new: $new"
if [[ $pausing == false ]]; then
execution=$(play $task_id)
if [[ -n "$execution" ]]; then
info "playing '$name' locally!!"
db LAST_TASK_ID "$(db CURR_TASK_ID)"
db CURR_TASK_ID "${task_id}"
db CURR_TASK_NAME "${name}"
else
err "2 - couldn't play '$name' ... execution:"
info "$execution"
fi
else
info "already paused locally"
fi
else
debug "...none finished"
if [[ ! -n "$task_id" ]]; then
debug "finding task id by last task with name..."
task=$($MYDIR/psql.sh "select t.id from tasks t where t.name = '$name' order by t.id desc limit 1")
task_id=$(echo $task | cut -d'|' -f1)
debug "found: $task_id"
fi
execution=$(pause $task_id)
if [[ -n "$execution" ]]; then
info "paused '$name' locally!"
$MYDIR/spend.sh "$task_id"
else
info "couldn't pause '$name' ..."
execution=$(play $task_id)
if [[ -n "$execution" ]]; then
info "playing '$name' locally!"
db LAST_TASK_ID "$(db CURR_TASK_ID)"
db CURR_TASK_ID "${task_id}"
db CURR_TASK_NAME "${name}"
else
err "1 - couldn't play '$name' ..."
fi
fi
fi
info -n "latest executions from '$name' (#$task_id):"
$MYDIR/psql.sh "select * from executions where task_id = $task_id order by id desc limit 5" --full
$MYDIR/elapsed.sh "$task_id"
# TODO
# - query last executions
# - comment insertions