Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to recover cancellations #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions scripts/recover-cancellations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#### 1. Download logs from cloudwatch

E.g.

`export AWS_DEFAULT_REGION=eu-west-1 && awslogs get /aws/lambda/support-reminders-cancel-reminders-PROD --start='2021-06-15 16:00' --end='2021-06-16 09:00' --filter-pattern="invalid input syntax for type uuid" --profile membership > filtered-dump.log`

#### 2. Extract the base64 codes from the error logs and decode

`grep "Error: error" filtered-dump.log > errors`

You can just use vim block edit mode to remove everything except the codes.

`cat errors | uniq > deduped`

`./decode.sh deduped`

#### 3. Lookup actual reminder_codes from postgres and send to cancellation endpoint

Setup the ssh tunnel to the database bastion, then run:
`node getReminderCode.js <database name> <username> <password> <path-to-file>`
5 changes: 5 additions & 0 deletions scripts/recover-cancellations/decode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
while IFS= read -r line; do
echo `echo $line | base64 -d`
done < "$1"

47 changes: 47 additions & 0 deletions scripts/recover-cancellations/getReminderCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const pg = require('pg');
const fs = require('fs');
const fetch = require('node-fetch');

const database = process.argv[2];
const user = process.argv[3];
const password = process.argv[4];
const path = process.argv[5];

const pool = new pg.Pool({
host: 'localhost',
database,
user,
password,
port: 5431,
});

const data = fs.readFileSync(path).toString()
data.split('\n').forEach(prefix => {
const query = {
text: `
WITH codes AS (
SELECT reminder_code
FROM recurring_reminder_signups
UNION
SELECT reminder_code
FROM one_off_reminder_signups
)
SELECT reminder_code FROM codes
WHERE
reminder_code::text LIKE($1)
`,
values: [`${prefix}%`],
};

pool.query(query)
.then(result => {
console.log(prefix, result.rows[0].reminder_code)
return fetch('https://reminders.support.guardianapis.com/cancel',
{
method: 'POST',
body: JSON.stringify({ reminderCode: result.rows[0].reminder_code} )
})
}).then(response => {
console.log('api response', response.status)
})
})
10 changes: 10 additions & 0 deletions scripts/recover-cancellations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "support-reminders",
"dependencies": {
"node-fetch": "^2.1.2",
"pg": "^7.9.0",
"pg-format": "^1.0.4"
},
"devDependencies": {},
"scripts": {}
}