Skip to content

Commit 4006597

Browse files
authored
Merge pull request #653 from praekeltfoundation/migrate-to-turn-scripts
Update readme wit queue update script
2 parents cefb518 + f65bd47 commit 4006597

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

scripts/migrate_to_turn/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ We can do this in batches and repeat until we do the actual switch over from Rap
77

88
## SCRIPTS
99

10+
There is one fetch script and 2 update script options. We can test out the different update option with larger batches and see which one works the best.
11+
1012
### fetch_rapidpro_contacts.py
1113

1214
This fetches all the contacts from Rapidpro based on the start and end date provided, you can also configure a limit.
@@ -19,6 +21,8 @@ It will also output the latest modified on date in the batch, this can then be u
1921

2022
### update_turn_contacts.py
2123

24+
Update using the turn contacts api asynchronously.
25+
2226
This script takes a filename of a file generated by the `fetch_rapidpro_contacts.py` script as a parameter and updates all the contact in the file on Turn.
2327

2428
It is an async script and `CONCURRENCY` can be updated to control the speed, to avoid hitting the Turn API rate limits.
@@ -28,6 +32,19 @@ Command to run:
2832

2933
The output is sent to a json file, which can be used to retry failed requests.
3034

35+
### update_turn_contacts_queue.py
36+
37+
Update using the turn contacts api asynchronously but using a queue and workers. It will sleep if it gets rate limited by turn.
38+
39+
This script takes a filename of a file generated by the `fetch_rapidpro_contacts.py` script as a parameter and updates all the contact in the file on Turn.
40+
41+
It is an async script and `WORKER_COUNT` can be configured, to change the amount being processed at a time.
42+
43+
Command to run:
44+
`python scripts/migrate_to_turn/update_turn_contacts_queue.py contacts-2025-01-01-2025-01-07.csv > update_turn_contacts.json`
45+
46+
The output is sent to a json file, which can be used to retry failed requests.
47+
3148
## FIELD_MAPPING
3249

3350
This is a dictionary the script uses to figure out where to get the data, how to process it and where it should go.

scripts/migrate_to_turn/fetch_rapidpro_contacts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_rapidpro_contacts(start_date=None, end_date=None):
6363

6464
if wa_id:
6565
data = get_field_data(contact)
66-
data["wa_id"] = wa_id
66+
data["urn"] = wa_id
6767
contacts.append(data)
6868

6969
modified_on = contact.modified_on.astimezone(pytz.utc)

scripts/migrate_to_turn/update_turn_contacts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
async def update_turn_contact_details(session, row, target):
17-
wa_id = row.pop("wa_id")
17+
wa_id = row.pop("urn")
1818

1919
url = urljoin(TURN_URL, f"/v1/contacts/{wa_id}/profile")
2020
headers = {

scripts/migrate_to_turn/update_turn_contacts_queue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def main(filename, target):
8282
tasks.append(task)
8383

8484
for row in reader:
85-
wa_id = row.pop("wa_id")
85+
wa_id = row.pop("urn")
8686
update = (session, wa_id, row, target)
8787
await queue.put(update)
8888

0 commit comments

Comments
 (0)