Skip to content

Commit

Permalink
Update proposition-tag-scorer lambda to new setup (#653)
Browse files Browse the repository at this point in the history
* Convert proposition-tag-scorer to new lambda setup

Signed-off-by: Carl Gieringer <[email protected]>

* Bump version proposition-tag-scorer: 1.1.0

Signed-off-by: Carl Gieringer <[email protected]>

* Standardize pg version

Signed-off-by: Carl Gieringer <[email protected]>

* Bump version proposition-tag-scorer: 1.2.0

Signed-off-by: Carl Gieringer <[email protected]>

* Add issues to TODOs

Signed-off-by: Carl Gieringer <[email protected]>

* Fix test todo command

Signed-off-by: Carl Gieringer <[email protected]>

* Update readme steps

Signed-off-by: Carl Gieringer <[email protected]>

---------

Signed-off-by: Carl Gieringer <[email protected]>
  • Loading branch information
carlgieringer authored Feb 20, 2024
1 parent 7e5c04e commit c90f6ef
Show file tree
Hide file tree
Showing 35 changed files with 184 additions and 229 deletions.
6 changes: 0 additions & 6 deletions howdju-service-common/lib/apiGateway/apiGateway.ts

This file was deleted.

6 changes: 6 additions & 0 deletions howdju-service-common/lib/awsHandlers/apiGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Context } from "aws-lambda";

export const configureHandlerContext = (context: Context) => {
// Otherwise the pg.Pool timeout keeps us alive
context.callbackWaitsForEmptyEventLoop = false;
};
2 changes: 1 addition & 1 deletion howdju-service-common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import momentDurationFormatSetup from "moment-duration-format";
anyPromiseRegister("bluebird", { Promise: require("bluebird") });
momentDurationFormatSetup(moment);

export * from "./apiGateway";
export * from "./awsHandlers";
export * from "./config";
export * from "./daos";
export * from "./database";
Expand Down
11 changes: 5 additions & 6 deletions howdju-service-common/lib/services/PropositionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import {
concat,
filter,
get,
has,
map,
reduce,
reject,
some,
toNumber,
unionBy,
Expand Down Expand Up @@ -484,13 +482,13 @@ export class PropositionsService {
const taggedNegativePropositionIds = reduce(
taggedNegativePropositions,
(acc, p) => {
acc[p.id] = true;
acc.add(p.id);
return acc;
},
{} as Record<EntityId, boolean>
new Set<EntityId>()
);
const prunedRecommendedPropositions = reject(recommendedPropositions, (p) =>
has(taggedNegativePropositionIds, p.id)
const prunedRecommendedPropositions = recommendedPropositions.filter(
(p) => !taggedNegativePropositionIds.has(p.id)
);
const propositions = unionBy(
taggedPositivePropositions,
Expand All @@ -500,6 +498,7 @@ export class PropositionsService {

return propositions;
}

private async readOrCreateEquivalentValidPropositionAsUser(
createProposition: CreateProposition,
userId: EntityId,
Expand Down
12 changes: 11 additions & 1 deletion lambdas/bin/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
node "${script_dir}"/../../bin/check-committed.mjs
"${script_dir}"/../../bin/check-pushed.sh

echo "Running checks…"
yarn run check-format
yarn run lint
yarn run typecheck
yarn run test

npm version minor
echo "Bumping version…"
yarn version minor
# npm version should commit (and tag) for us, but it won't when the package
# is in a subdirectory of the Git repo: https://github.com/npm/npm/issues/9111
# So create the commit here.
Expand All @@ -21,13 +25,19 @@ git commit -m "Bump version ${lambda_name}: ${lambda_version}" -s
git tag "versions/${lambda_name}-${lambda_version}"
git push

echo "Cleaning build…"
yarn run clean
echo "Building…"
yarn run build

echo "Zipping lambda…"
extra_file=$1
if [[ -n $extra_file ]]; then
"${script_dir}"/../../bin/build-and-run-script.sh "${script_dir}"/zip-lambda.js --extra-file=$extra_file
else
"${script_dir}"/../../bin/build-and-run-script.sh "${script_dir}"/zip-lambda.js
fi

echo "Uploading lambda…"
"${script_dir}"/../../bin/build-and-run-script.sh "${script_dir}"/upload-lambda.js
echo "Done with release."
3 changes: 2 additions & 1 deletion lambdas/howdju-message-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"lint": "eslint --ignore-path=.gitignore .",
"release": "../bin/release.sh ../../howdju-text-fragments/dist/rangeToFragment.js",
"test": "jest",
"test-inspect": "node --inspect-brk ../../node_modules/.bin/jest --runInBand --watch"
"test-inspect": "node --inspect-brk ../../node_modules/.bin/jest --runInBand --watch",
"typecheck": "tsc"
},
"author": "Carl Gieringer <[email protected]>",
"license": "AGPL-3.0-or-later",
Expand Down
3 changes: 1 addition & 2 deletions lambdas/howdju-message-handler/src/LambdaProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
DatabaseProvider,
AwsProvider,
ServicesProvider,
DaosProvider,
baseConfig,
BaseProvider,
} from "howdju-service-common";
Expand All @@ -34,10 +33,10 @@ export class LambdaProvider implements BaseProvider {
assign(this, { appConfig: baseConfig });
assign(this, databaseInit(this as unknown as ConfigProvider));
assign(this, daosInitializer(this as unknown as DatabaseProvider));
assign(this, searchersInitializer(this as unknown as DaosProvider));
assign(this, validatorsInitializer(this as unknown as LoggerProvider));
assign(this, awsInit(this as unknown as LoggerProvider));
assign(this, servicesInitializer(this as unknown as AwsProvider));
assign(this, searchersInitializer(this as unknown as ServicesProvider));

(this as unknown as LoggerProvider).logger.debug(
"AppProvider initialization complete"
Expand Down
12 changes: 12 additions & 0 deletions lambdas/proposition-tag-scorer/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
overrides: [
{
files: ["**/*.{js,ts}"],
excludedFiles: ["node_modules/**"],
extends: ["howdju/node"],
},
],
parserOptions: {
project: ["./tsconfig.json"],
},
};
2 changes: 2 additions & 0 deletions lambdas/proposition-tag-scorer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist/
/coverage/
17 changes: 17 additions & 0 deletions lambdas/proposition-tag-scorer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# howdju-message-handler

This lambda scores proposition tag votes. It should be run on a schedule.

## Releasing

```sh
aws-vault exec username@howdju -- yarn run release
```

(See the section in Development.md about setting up aws-vault.)

Then:

- Update the Lambda's code in the AWS console by copy/pasting the new ZIP file's S3 location.
- Publish a new version of the Lambda
- Update the `prod` alias to point to the new version.
2 changes: 2 additions & 0 deletions lambdas/proposition-tag-scorer/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import baseConfig from "../../jest.config.base";
export default baseConfig;
31 changes: 31 additions & 0 deletions lambdas/proposition-tag-scorer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "proposition-tag-scorer",
"version": "1.2.0",
"description": "Scores tags for propositions",
"main": "index.ts",
"repository": "[email protected]:howdju/howdju.git",
"author": "Carl Gieringer <[email protected]>",
"license": "AGPL-3.0-or-later",
"scripts": {
"build": "node ../../premiser-api/bin/esbuild.js --entryPoint=src/handler.ts --outfile=dist/index.js",
"check-format": "yarn run prettier --check --ignore-path .gitignore .",
"clean": "rm -rf dist",
"fix-format": "yarn run lint --fix && yarn run prettier --write --ignore-path .gitignore .",
"lint": "eslint --ignore-path=.gitignore .",
"release": "../bin/release.sh ../../howdju-text-fragments/dist/rangeToFragment.js",
"test": "echo 'TODO(#654) add tests'",
"test-inspect": "echo 'TODO(#654) add tests'",
"typecheck": "tsc"
},
"dependencies": {
"pg": "^8.6.0"
},
"devDependencies": {
"esbuild": "^0.18.17",
"eslint": "^8.27.0",
"eslint-config-howdju": "workspace:eslint-config-howdju",
"jest": "^29.3.1",
"prettier": "2.7.1",
"typescript": "4.9.4"
}
}
15 changes: 15 additions & 0 deletions lambdas/proposition-tag-scorer/src/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Context, ScheduledEvent } from "aws-lambda";

import { configureHandlerContext } from "howdju-service-common";

import { propositionTagScoresService, logger } from "./initialization";

export async function handler(event: ScheduledEvent, context: Context) {
configureHandlerContext(context);

logger.silly({ gatewayContext: context, event });

logger.info("Scoring propositions…");
await propositionTagScoresService.updatePropositionTagScoresUsingUnscoredVotes();
logger.info("Scoring proposition tags succeeded.");
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pg from "pg";
import { toNumber } from "lodash";

import {
Database,
makeTimestampToUtcMomentParser,
PgTypeOids,
} from "howdju-service-common";

import { logger } from "./loggerInitialization";

pg.types.setTypeParser(
PgTypeOids.TIMESTAMP,
makeTimestampToUtcMomentParser(logger)
);

const config = {
user: process.env["DB_USER"],
database: process.env["DB_NAME"],
password: process.env["DB_PASSWORD"],
host: process.env["DB_HOST"],
port: toNumber(process.env["DB_PORT"]) || 5432,
max: toNumber(process.env["DB_POOL_MAX_CLIENTS"]) || 10,
idleTimeoutMillis: toNumber(process.env["DB_CLIENT_IDLE_TIMEOUT"]) || 10000,
connectionTimeoutMillis:
toNumber(process.env["DB_CLIENT_CONNECT_TIMEOUT"]) || 5000,
};

export const pool = new pg.Pool(config);
pool.on("error", (err) => logger.error("database pool error", { err }));

export const database = new Database(logger, pool);
1 change: 1 addition & 0 deletions lambdas/proposition-tag-scorer/src/initialization/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./initialization";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./loggerInitialization";
export * from "./servicesInitialization";
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* This file must pass console to the Logger */
/* eslint "no-console": ["off"] */

const { AwsLogger } = require("howdju-service-common");
import { Logger } from "howdju-common";
import { AwsLogger } from "howdju-service-common";

const logLevel = process.env.LOG_LEVEL || "warn";
const isAws = !!process.env.IS_AWS;
Expand All @@ -11,11 +12,11 @@ const logFormat = isAws ? "json" : "text";

console.log("initializing logger");

exports.logger = new AwsLogger(console, {
export const logger = new AwsLogger(console, {
logLevel,
doLogTimestamp,
doUseCarriageReturns,
logFormat,
});
}) as unknown as Logger;

exports.logger.debug("initializing logger", { logLevel, isAws });
logger.debug("initializing logger", { logLevel, isAws });
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const {
import {
PropositionTagScoresService,
PropositionTagScoresDao,
PropositionTagVotesDao,
JobHistoryDao,
} = require("howdju-service-common");
} from "howdju-service-common";

const { logger } = require("./loggerInitialization");
const { database } = require("./databaseInitialization");
import { logger } from "./loggerInitialization";
import { database } from "./databaseInitialization";

logger.debug("Initializing services");
const propositionTagScoresDao = new PropositionTagScoresDao(logger, database);
const jobHistoryDao = new JobHistoryDao(logger, database);
const propositionTagVotesDao = new PropositionTagVotesDao(logger, database);
exports.propositionTagScoresService = new PropositionTagScoresService(
const propositionTagVotesDao = new PropositionTagVotesDao(database);
export const propositionTagScoresService = new PropositionTagScoresService(
logger,
propositionTagScoresDao,
jobHistoryDao,
Expand Down
9 changes: 9 additions & 0 deletions lambdas/proposition-tag-scorer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"paths": {
// Support project-relative imports
"@/*": ["./lib/*"]
}
}
}
2 changes: 1 addition & 1 deletion premiser-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"howdju-service-routes": "workspace:howdju-service-routes",
"moment": "^2.29.4",
"path-to-regexp": "^6.2.1",
"pg": "^8.5.1",
"pg": "^8.6.0",
"source-map-support": "^0.5.19",
"uuid": "^9.0.0"
}
Expand Down
4 changes: 2 additions & 2 deletions premiser-api/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import {
AppProvider,
AwsLogger,
configureGatewayContext,
configureHandlerContext,
} from "howdju-service-common";

import { routeRequest } from "./router";
Expand All @@ -58,7 +58,7 @@ export function handler(
gatewayCallback: APIGatewayProxyCallback
) {
try {
configureGatewayContext(gatewayContext);
configureHandlerContext(gatewayContext);

const appProvider = getOrCreateAppProvider(gatewayEvent);

Expand Down
2 changes: 1 addition & 1 deletion premiser-migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"bluebird": "^3.5.0",
"lodash": "^4.17.4",
"mysql": "^2.13.0",
"pg": "^8.11.3"
"pg": "^8.6.0"
},
"devDependencies": {
"env-cmd": "^10.1.0",
Expand Down
10 changes: 5 additions & 5 deletions premiser-processing/lambda-build.dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM library/amazonlinux:2018.03
FROM library/amazonlinux

# TODO: replace with esbuild bundling like in premiser-api

RUN \
yum -y update && \
yum -y install \
git \
# necessary to build bcrypt
gcc-c++ make \
&& \
git \
# necessary to build bcrypt
gcc-c++ make \
&& \
# configuring git allows us to stash changes
git config --global user.email "howdju@docker" && \
git config --global user.name "howdju@docker" && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Carl Gieringer <[email protected]>",
"license": "AGPL-3.0-or-later",
"dependencies": {
"pg": "^7.2.0"
"pg": "^8.6.0"
},
"scripts": {
"check-format": "yarn run prettier --check --ignore-path .gitignore .",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { configureGatewayContext } = require("howdju-service-common");
const { configureHandlerContext } = require("howdju-service-common");

const { justificationScoresService, logger } = require("./initialization");

exports.handler = (gatewayEvent, gatewayContext, gatewayCallback) => {
configureGatewayContext(gatewayContext);
configureHandlerContext(gatewayContext);

logger.silly({ gatewayEvent });
logger.silly({ gatewayContext });
Expand Down

This file was deleted.

Loading

0 comments on commit c90f6ef

Please sign in to comment.