Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(name: string, memory: number = 128) {
let redis = new cloud.Service(name, {
containers: {
redis: {
image: "redis:alpine",
memory: memory,
ports: [{ port: 6379 }],
command: ["redis-server", "--requirepass", redisPassword],
},
},
});
this.get = (key: string) => {
return redis.getEndpoint("redis", 6379).then(endpoint => {
// console.log(`Endpoint: ${JSON.stringify(endpoint)}`);
let client = require("redis").createClient(
endpoint.port,
endpoint.hostname,
{ password: redisPassword },
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cloud from "@pulumi/cloud";
import * as nodeFetchModule from "node-fetch";
import { canonicalUrl, hostname} from "./support";
import * as express from "express";
// Pending sites to be processed
let sites = new cloud.Topic("examples-sites-to-process");
// Documents and associated metadata for crawled sites
let documents = new cloud.Table("examples-documents");
// Front end API and console
let frontEnd = new cloud.HttpServer("examples-crawler-front-end", () => {
const app = express();
app.post("/queue", async (req, res) => {
let url = req.body.toString();
console.log(`Pushing ${url} to processing queue`);
await sites.publish(url);
res.status(200).json("success");
});
app.get("/documents/stats", async (_, res) => res.json({count: (await documents.scan()).length}));
return app;
});
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
import * as cloud from "@pulumi/cloud";
import * as cache from "./cache";
// Create a web server.
let endpoint = new cloud.HttpEndpoint("urlshortener");
// Create a table `urls`, with `name` as primary key.
let urlTable = new cloud.Table("urls", "name");
// Create a cache of frequently accessed urls.
let urlCache = new cache.Cache("urlcache");
// Serve all files in the www directory to the root.
endpoint.static("/", "www");
// GET /url lists all URLs currently registered.
endpoint.get("/url", async (req, res) => {
try {
let items = await urlTable.scan();
res.status(200).json(items);
console.log(`GET /url retrieved ${items.length} items`);
} catch (err) {
res.status(500).json(err.stack);
console.log(`GET /url error: ${err.stack}`);
var v: TemplateStringsArray
import * as cloud from "@pulumi/cloud";
import { authMiddleware } from "./middleware";
import * as express from "express";
import * as bodyParser from "body-parser";
type AsyncRequestHandler = (req: express.Request, res: express.Response, next: express.NextFunction) => Promise;
const asyncMiddleware = (fn: AsyncRequestHandler) =>
(req: express.Request, res: express.Response, next: express.NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
let todos = new cloud.Table("examples-todo");
let server = new cloud.HttpServer("examples-todo", () => {
const app = express();
// Serve all files in the 'www' folder under '/'
// 'index.html' will be automatically served as '/' as well as '/index.html'.
app.use("/", express.static("www"));
// GET/POST todo handlers
app.get("/todo/:id", authMiddleware, asyncMiddleware(async (req, res) => {
console.log("GET /todo/" + req.params["id"]);
try {
let item = await todos.get({ id: req.params["id"] });
res.status(200).json(item.value);
} catch (err) {
res.status(500).json(err);
}
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cloud from "@pulumi/cloud";
// A poll function takes an opaque token generated from the last execution (or undefined), and
// returns any new items since the last invocation, along with a new token to be used with the
// next invocation.
export type PollFunction = (lastToken?: string) => Promise<{ items: T[]; nextToken: string; }>;
let pollMarkers = new cloud.Table("__pollMarkers");
// poll represents a stream of items which are derived from polling at a given rate
// using a user-provided polling function.
export function poll(name: string, rate: cloud.timer.IntervalRate, poller: PollFunction): cloud.Stream {
let topic = new cloud.Topic(name);
cloud.timer.interval(name, rate, async () => {
console.log(`Starting polling...`);
console.log(`Getting pollMarker for ${name}`);
let pollMarker = await pollMarkers.get({id: name});
console.log(`pollMarker is ${JSON.stringify(pollMarker, null, "")}`);
let lastToken: string | undefined;
if (pollMarker !== undefined) {
lastToken = pollMarker.lastToken;
export function exampleTwitter2() {
console.log("Running Twitter example 2...");
// Get a stream of all tweets matching this query, forever...
let tweets: cloud.Stream = twitter.search("pulumi", "vscode");
// Collect them into bunches
let digest = new Digest("tweetdigest", tweets);
// Every night, take all of the tweets collected since the
// last digest and publish that as a group to the digest stream.
cloud.timer.daily("nightly", { hourUTC: 7 }, async () => {
await digest.collect();
});
// For every group of tweets published to the digest stream (nightly)
// send an email.
digest.subscribe("digest", async (dailyTweets) => {
if (dailyTweets.length === 0) {
console.log("No new tweets...");
return;
}
console.log(`Received ${dailyTweets.length} new tweets. Sending email...`);
// Arbitrary code to compose email body - could use templating system or
// any other programmatic way of constructing the text.
let text = "Tweets:\n";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as pulumi from "@pulumi/cloud";
let countDown = new pulumi.Topic("examples-countDown");
countDown.subscribe("watcher", async (num) => {
console.log(num);
if (num > 0) {
await countDown.publish(num - 1);
}
});
pulumi.timer.interval("examples-heartbeat", {minutes: 3}, async () => {
await countDown.publish(25);
});
found = await documents.get({id: link});
if (!found) {
console.log(`${url}: Publishing new url: ${link}`);
await sites.publish(link);
}
}
// Register that we completed publishing all referened URLs from this site. We can recover from
// failures by re-crawling any sites with `crawlInProgress == true`.
await documents.update({ id: url }, { crawlInProgress: false, crawlFinishedDate: Date.now() });
console.log("Succeed url: " + url);
});
// Periodically check for any URLs which did not complete processing
// and try them again (for example, in case they failed due to throttling).
cloud.timer.interval("cleanup", {minutes: 5}, async () => {
console.log(`Cleanup: starting cleanup.`);
let cleanupCount = 0;
let alldocs = await documents.scan();
console.log(`Cleanup: scan returned ${alldocs.length} documents.`);
for (let doc of alldocs) {
if (doc.crawlInProgress) {
await sites.publish(doc.id);
cleanupCount++;
}
}
console.log(`Cleanup: restarted ${cleanupCount} items.`);
});
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cloud from "@pulumi/cloud";
import * as pulumi from "@pulumi/pulumi";
let config = new pulumi.Config("timers:config");
let message = config.require("message");
cloud.timer.interval("examples-test-interval1", { minutes: 1 }, async () => {
console.log(`test-interval[${Date.now()}]: ${message}`);
});
cloud.timer.interval("examples-test-interval2", { minutes: 59 }, async () => {
console.log(`test-interval[${Date.now()}]: ${message}`);
});
cloud.timer.interval("examples-test-interval3", { minutes: 120 }, async () => {
console.log(`test-interval[${Date.now()}]: ${message}`);
});
cloud.timer.interval("examples-test-interval4", { minutes: 120, hours: 2 }, async () => {
console.log(`test-interval[${Date.now()}]: ${message}`);
});
cloud.timer.interval("examples-test-interval5", { days: 24 }, async () => {
// build an anonymous image:
let builtService = new cloud.Service("examples-nginx2", {
containers: {
nginx: {
build: "./app",
memory: 128,
ports: [{ port: 80 }],
},
},
replicas: 2,
waitForSteadyState: false,
});
// expose some APIs meant for testing purposes.
let api = new cloud.API("examples-containers");
api.get("/test", async (req, res) => {
try {
res.json({
nginx: await nginx.getEndpoint(),
nginx2: await builtService.getEndpoint(),
});
} catch (err) {
console.error(errorJSON(err));
res.status(500).json(errorJSON(err));
}
});
function errorJSON(err: any) {
const result: any = Object.create(null);
Object.getOwnPropertyNames(err).forEach(key => result[key] = err[key]);
return result;