Skip to content

Commit

Permalink
Update sample to not cause unserializable captures. (pulumi#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Feb 15, 2020
1 parent 8480e6e commit 1e1720f
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions aws-ts-url-shortener-cache-http/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class Cache {
private readonly redis: awsx.ecs.FargateService;
private readonly endpoint: pulumi.Output<awsx.elasticloadbalancingv2.ListenerEndpoint>;

public get: (key: string) => Promise<string>;
public set: (key: string, value: string) => Promise<void>;

constructor(name: string, memory: number = 128) {
const pw = config.redisPassword;
const listener = new awsx.elasticloadbalancingv2.NetworkListener(name, { port: 6379 });
Expand All @@ -26,38 +29,41 @@ export class Cache {
});

this.endpoint = listener.endpoint;
}

public get(key: string): Promise<string> {
const ep = this.endpoint.get();
console.log(`Getting key '${key}' on Redis@${ep.hostname}:${ep.port}`);

const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
return new Promise<string>((resolve, reject) => {
client.get(key, (err: any, v: any) => {
if (err) {
reject(err);
} else {
resolve(v);
}
// Expose get/set as member fields that don't capture 'this'. That way we don't try to
// serialize pulumi resources unnecessarily into our lambdas.
this.get = key => {
const ep = listener.endpoint.get();
console.log(`Getting key '${key}' on Redis@${ep.hostname}:${ep.port}`);

const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
return new Promise<string>((resolve, reject) => {
client.get(key, (err: any, v: any) => {
if (err) {
reject(err);
} else {
resolve(v);
}
});
});
});
}
};

this.set = (key, value) => {
const ep = listener.endpoint.get();
console.log(`Setting key '${key}' to '${value}' on Redis@${ep.hostname}:${ep.port}`);

public set(key: string, value: string): Promise<void> {
const ep = this.endpoint.get();
console.log(`Setting key '${key}' to '${value}' on Redis@${ep.hostname}:${ep.port}`);

const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
return new Promise<void>((resolve, reject) => {
client.set(key, value, (err: any, v: any) => {
if (err) {
reject(err);
} else {
console.log("Set succeed: " + JSON.stringify(v));
resolve();
}
const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
return new Promise<void>((resolve, reject) => {
client.set(key, value, (err: any, v: any) => {
if (err) {
reject(err);
} else {
console.log("Set succeed: " + JSON.stringify(v));
resolve();
}
});
});
});
};
}
}

0 comments on commit 1e1720f

Please sign in to comment.