with cronjob for regular user checking

This commit is contained in:
lingdocs 2021-09-18 09:19:44 -04:00
parent 6cff201f8a
commit bf21b3a0a7
3 changed files with 41 additions and 0 deletions

View File

@ -16,6 +16,7 @@
"@types/react-router-dom": "^5.1.9", "@types/react-router-dom": "^5.1.9",
"bootstrap": "4.5.3", "bootstrap": "4.5.3",
"classnames": "^2.2.6", "classnames": "^2.2.6",
"cron": "^1.8.2",
"markdown-to-jsx": "^7.1.3", "markdown-to-jsx": "^7.1.3",
"react": "^17.0.1", "react": "^17.0.1",
"react-bootstrap": "^1.5.2", "react-bootstrap": "^1.5.2",
@ -58,6 +59,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/plugin-transform-react-jsx": "^7.14.5", "@babel/plugin-transform-react-jsx": "^7.14.5",
"@types/cron": "^1.7.3",
"mdx-loader": "^3.0.2", "mdx-loader": "^3.0.2",
"node-fetch": "^2.6.1" "node-fetch": "^2.6.1"
} }

View File

@ -1,5 +1,6 @@
import React, { useState, createContext, useEffect } from "react" import React, { useState, createContext, useEffect } from "react"
import { AT, getUser } from "@lingdocs/lingdocs-main"; import { AT, getUser } from "@lingdocs/lingdocs-main";
import { CronJob } from "cron";
const UserContext = createContext< const UserContext = createContext<
{ {
@ -10,17 +11,28 @@ const UserContext = createContext<
| undefined | undefined
>(undefined); >(undefined);
// TODO: persisting user in local state
function UserProvider({ children }: any) { function UserProvider({ children }: any) {
const [user, setUser] = useState<AT.LingdocsUser | undefined>(undefined); const [user, setUser] = useState<AT.LingdocsUser | undefined>(undefined);
function pullUser() { function pullUser() {
console.log("pulling user...");
getUser().then((user) => { getUser().then((user) => {
setUser(user === "offline" ? undefined : user); setUser(user === "offline" ? undefined : user);
}).catch(console.error); }).catch(console.error);
} }
const checkUserCronJob = new CronJob("1/30 * * * * *", () => {
pullUser();
});
useEffect(() => { useEffect(() => {
pullUser(); pullUser();
checkUserCronJob.start();
return () => {
checkUserCronJob.stop();
}
// eslint-disable-next-line
}, []); }, []);
return <UserContext.Provider value={{ user, setUser, pullUser }}> return <UserContext.Provider value={{ user, setUser, pullUser }}>

View File

@ -1890,6 +1890,14 @@
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
"@types/cron@^1.7.3":
version "1.7.3"
resolved "https://registry.yarnpkg.com/@types/cron/-/cron-1.7.3.tgz#993db7d54646f61128c851607b64ba4495deae93"
integrity sha512-iPmUXyIJG1Js+ldPYhOQcYU3kCAQ2FWrSkm1FJPoii2eYSn6wEW6onPukNTT0bfiflexNSRPl6KWmAIqS+36YA==
dependencies:
"@types/node" "*"
moment ">=2.14.0"
"@types/eslint-visitor-keys@^1.0.0": "@types/eslint-visitor-keys@^1.0.0":
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@ -3889,6 +3897,13 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4:
safe-buffer "^5.0.1" safe-buffer "^5.0.1"
sha.js "^2.4.8" sha.js "^2.4.8"
cron@^1.8.2:
version "1.8.2"
resolved "https://registry.yarnpkg.com/cron/-/cron-1.8.2.tgz#4ac5e3c55ba8c163d84f3407bde94632da8370ce"
integrity sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==
dependencies:
moment-timezone "^0.5.x"
cross-spawn@7.0.1: cross-spawn@7.0.1:
version "7.0.1" version "7.0.1"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14"
@ -7938,6 +7953,18 @@ mkdirp@^0.5.5:
dependencies: dependencies:
minimist "^1.2.5" minimist "^1.2.5"
moment-timezone@^0.5.x:
version "0.5.33"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.33.tgz#b252fd6bb57f341c9b59a5ab61a8e51a73bbd22c"
integrity sha512-PTc2vcT8K9J5/9rDEPe5czSIKgLoGsH8UNpA4qZTVw0Vd/Uz19geE9abbIOQKaAQFcnQ3v5YEXrbSc5BpshH+w==
dependencies:
moment ">= 2.9.0"
"moment@>= 2.9.0", moment@>=2.14.0:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
move-concurrently@^1.0.1: move-concurrently@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"