Skip to content

Commit

Permalink
Merge pull request nyaruka#59 from nyaruka/use_run_status
Browse files Browse the repository at this point in the history
Use run `status` instead of `is_active` and `exit_type`
  • Loading branch information
rowanseymour authored Jan 6, 2022
2 parents cbaebfe + 1acd9b3 commit 68a8b45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
32 changes: 19 additions & 13 deletions archives/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import (
"github.com/sirupsen/logrus"
)

const (
RunStatusActive = "A"
RunStatusWaiting = "W"
RunStatusCompleted = "C"
RunStatusExpired = "X"
RunStatusInterrupted = "I"
RunStatusFailed = "F"
)

const lookupFlowRuns = `
SELECT rec.exited_on, row_to_json(rec)
FROM (
Expand All @@ -33,14 +42,11 @@ FROM (
fr.modified_on,
fr.exited_on,
CASE
WHEN exit_type = 'C'
THEN 'completed'
WHEN exit_type = 'I'
THEN 'interrupted'
WHEN exit_type = 'E'
THEN 'expired'
ELSE
null
WHEN status = 'C' THEN 'completed'
WHEN status = 'I' THEN 'interrupted'
WHEN status = 'X' THEN 'expired'
WHEN status = 'F' THEN 'failed'
ELSE NULL
END as exit_type,
a.username as submitted_by
Expand Down Expand Up @@ -87,7 +93,7 @@ func writeRunRecords(ctx context.Context, db *sqlx.DB, archive *Archive, writer
}

const selectOrgRunsInRange = `
SELECT fr.id, fr.is_active
SELECT fr.id, fr.status
FROM flows_flowrun fr
LEFT JOIN contacts_contact cc ON cc.id = fr.contact_id
WHERE fr.org_id = $1 AND fr.modified_on >= $2 AND fr.modified_on < $3
Expand Down Expand Up @@ -148,18 +154,18 @@ func DeleteArchivedRuns(ctx context.Context, config *Config, db *sqlx.DB, s3Clie
defer rows.Close()

var runID int64
var isActive bool
var status string
runCount := 0
runIDs := make([]int64, 0, archive.RecordCount)
for rows.Next() {
err = rows.Scan(&runID, &isActive)
err = rows.Scan(&runID, &status)
if err != nil {
return err
}

// if this run is still active, something has gone wrong, throw an error
if isActive {
return fmt.Errorf("run %d in archive is still active", runID)
if status == RunStatusActive || status == RunStatusWaiting {
return fmt.Errorf("run #%d in archive hadn't exited", runID)
}

// increment our count
Expand Down
16 changes: 7 additions & 9 deletions testdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ DROP TABLE IF EXISTS flows_actionlog CASCADE;
DROP TABLE IF EXISTS flows_flowrun CASCADE;
CREATE TABLE flows_flowrun (
id serial primary key,
is_active boolean NOT NULL DEFAULT FALSE,
uuid character varying(36) NOT NULL UNIQUE,
responded boolean NOT NULL,
contact_id integer NOT NULL references contacts_contact(id),
Expand All @@ -179,7 +178,6 @@ CREATE TABLE flows_flowrun (
exited_on timestamp with time zone NULL,
submitted_by_id integer NULL references auth_user(id),
status varchar(1) NOT NULL,
exit_type varchar(1) NULL,
delete_reason char(1) NULL
);

Expand Down Expand Up @@ -306,25 +304,25 @@ INSERT INTO flows_flow(id, uuid, name) VALUES
INSERT INTO auth_user(id, username) VALUES
(1, '[email protected]');

INSERT INTO flows_flowrun(id, uuid, responded, contact_id, flow_id, org_id, results, path, events, created_on, modified_on, exited_on, status, exit_type, submitted_by_id) VALUES
(1, '4ced1260-9cfe-4b7f-81dd-b637108f15b9', TRUE, 6, 1, 2, '{}', '[]', '[]', '2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', 'C', NULL),
INSERT INTO flows_flowrun(id, uuid, responded, contact_id, flow_id, org_id, results, path, events, created_on, modified_on, exited_on, status, submitted_by_id) VALUES
(1, '4ced1260-9cfe-4b7f-81dd-b637108f15b9', TRUE, 6, 1, 2, '{}', '[]', '[]', '2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', NULL),
(2, '7d68469c-0494-498a-bdf3-bac68321fd6d', TRUE, 6, 1, 2,
'{"agree": {"category": "Strongly agree", "node_uuid": "a0434c54-3e26-4eb0-bafc-46cdeaf435ac", "name": "Do you agree?", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}}',
'[{"uuid": "c3d0b417-db75-417c-8050-33776ec8f620", "node_uuid": "10896d63-8df7-4022-88dd-a9d93edf355b", "arrived_on": "2017-08-12T15:07:24.049815+02:00", "exit_uuid": "2f890507-2ad2-4bd1-92fc-0ca031155fca"}]',
'[{"msg": {"urn": "tel:+12076661212", "text": "hola", "uuid": "cf05c58f-31fb-4ce8-9e65-4ecc9fd47cbe", "channel": {"name": "1223", "uuid": "bbfe2e9c-cf69-4d0a-b42e-00ac3dc0b0b8"}}, "type": "msg_created", "step_uuid": "659cdae5-1f29-4a58-9437-10421f724268", "created_on": "2018-01-22T15:06:47.357682+00:00"}]',
'2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', 'C', NULL),
'2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', NULL),
(3, 'de782b35-a398-46ed-8550-34c66053841b', TRUE, 7, 2, 3,
'{"agree": {"category": "Strongly agree", "node_uuid": "084c8cf1-715d-4d0a-b38d-a616ed74e638", "name": "Agree", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}, "confirm_agree": {"category": "Confirmed Strongly agree", "node_uuid": "a0434c54-3e26-4eb0-bafc-46cdeaf435ab", "name": "Do you agree?", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}}',
'[{"uuid": "600ac5b4-4895-4161-ad97-6e2f1bb48bcb", "node_uuid": "accbc6e2-b0df-46cd-9a76-bff0fdf4d753", "arrived_on": "2017-08-12T15:07:24.049815+02:00", "exit_uuid": "8249e2dc-c893-4200-b6d2-398d07a459bc"}]',
'[{"msg": {"urn": "tel:+12076661212", "text": "hola", "uuid": "9ea50923-0888-4596-9a9d-4890994934a9", "channel": {"name": "1223", "uuid": "d6597e08-8285-428c-8e7e-97c68adfa073"}}, "type": "msg_created", "step_uuid": "ae067248-df92-41c8-bb29-92506e984259", "created_on": "2018-01-22T15:06:47.357682+00:00"}]',
'2017-08-10 21:11:59.890662+02:00','2017-08-10 21:11:59.890662+02:00','2017-08-10 21:11:59.890662+02:00', 'C', 'C', 1),
'2017-08-10 21:11:59.890662+02:00','2017-08-10 21:11:59.890662+02:00','2017-08-10 21:11:59.890662+02:00', 'C', 1),
(4, '329a5d24-64fc-479c-8d24-9674c9b46530', TRUE, 7, 2, 3,
'{"agree": {"category": "Disagree", "node_uuid": "084c8cf1-715d-4d0a-b38d-a616ed74e638", "name": "Agree", "value": "B", "created_on": "2017-10-10T12:25:21.714339+00:00", "input": "B"}}',
'[{"uuid": "babf4fc8-e12c-4bb9-a9dd-61178a118b5a", "node_uuid": "accbc6e2-b0df-46cd-9a76-bff0fdf4d753", "arrived_on": "2017-10-12T15:07:24.049815+02:00", "exit_uuid": "8249e2dc-c893-4200-b6d2-398d07a459bc"}]',
'[{"msg": {"urn": "tel:+12076661212", "text": "hi hi", "uuid": "543d2c4b-ff0b-4b87-a9a4-b2d6745cf470", "channel": {"name": "1223", "uuid": "d6597e08-8285-428c-8e7e-97c68adfa073"}}, "type": "msg_created", "step_uuid": "3a5014dd-7b14-4b7a-be52-0419c09340a6", "created_on": "2018-10-12T15:06:47.357682+00:00"}]',
'2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00', 'C', 'C', NULL),
(5, 'abed67d2-06b8-4749-8bb9-ecda037b673b', TRUE, 7, 2, 3, '{}', '[]', '[]', '2017-10-10 21:11:59.890663+02:00','2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00', 'C', 'C', NULL),
(6, '6262eefe-a6e9-4201-9b76-a7f25e3b7f29', TRUE, 7, 2, 3, '{}', '[]', '[]', '2017-12-12 21:11:59.890662+02:00','2017-12-12 21:11:59.890662+02:00','2017-12-12 21:11:59.890662+02:00', 'C', 'C', NULL);
'2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00', 'C', NULL),
(5, 'abed67d2-06b8-4749-8bb9-ecda037b673b', TRUE, 7, 2, 3, '{}', '[]', '[]', '2017-10-10 21:11:59.890663+02:00','2017-10-10 21:11:59.890662+02:00','2017-10-10 21:11:59.890662+02:00', 'C', NULL),
(6, '6262eefe-a6e9-4201-9b76-a7f25e3b7f29', TRUE, 7, 2, 3, '{}', '[]', '[]', '2017-12-12 21:11:59.890662+02:00','2017-12-12 21:11:59.890662+02:00','2017-12-12 21:11:59.890662+02:00', 'C', NULL);

INSERT INTO flows_flowpathrecentrun(id, run_id) VALUES
(1, 3);
Expand Down

0 comments on commit 68a8b45

Please sign in to comment.