Skip to content
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
16 changes: 16 additions & 0 deletions .github/workflows/cronjob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Ping Server

on:
schedule:
- cron: "*/10 * * * *"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this frequent enough? Need to make sure you account for variations in cronjob startup time. If it shuts down after 15 min of inactivity then this 10 min timer should be fine. If it shuts down after 10 min of inactivity, you should shorten a bit e.g. 8 min


jobs:
ping:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Run Ping Script
run: /usr/bin/node /server/serverPing.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to run npm i to install dependencies? Should these dependencies include the full project or just the two from pingServer?

43 changes: 34 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"dependencies": {
"@babel/runtime": "^7.22.6",
"bootstrap": "^5.3.2",
"child_process": "^1.0.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"helmet": "^7.0.0",
Expand All @@ -87,6 +88,7 @@
"react-player": "^2.13.0",
"react-range": "^1.8.14",
"react-slider": "^2.0.6",
"util": "^0.12.5",
"winston": "^3.10.0"
},
"engines": {
Expand Down
1 change: 1 addition & 0 deletions server/cronjob.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/10 * * * * /usr/bin/node /server/serverPing.js
15 changes: 15 additions & 0 deletions server/serverPing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const child_process = require("child_process"); //to run command-line tools
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to run JavaScript for this? Or could you just run ping $URL on the command line directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Matthew, thank you for all your comments. Really appreciate it! I think the only dependencies that should be included are the ones from pingServer. For running ping $URL on the command line directly, I wasn't sure how to do it, so I will have a read and change it. If you have any documentation recommendation that would be great.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing @munozirianni1988 - we're getting to the meat and potatoes of software engineering now!

I would look at curl for making HTTP requests from the command line, e.g.:

curl https://google.com

To make sure the response is good, you could look at HTTP status codes.

To see the requests that curl is making - or any app for that matter - you can use HTTPdump. It allows you to create a fake website or API endpoint and see what your code is sending on requests.

I'd also recommend looking at the monthly usage limits for Render to make sure that you can run the app all the time without running out of usage. You get 750 hours / month (31.25 days) but if those hours cover both the server and the database, you would only get 15.125 days for those two instances.

If you can't run it 24 hours a day, I'd recommend running your cronjob only during the 12 hours you think it will be most useful (e.g. 8am - 8pm every day, or 14 hours a day but it's off on Sundays). You could add additional optimisation by caching the database response in the API server, so we only hit the database once on startup (e.g. with node-fetch-cache). This would allow the database to be off most of the time, and the data only changes on deployment so it wouldn't get out-of-date.

Also make sure you look at the Github Actions free tier limits to make sure your cronjob will run all month. It may also help you ask questions like "Is it better to have a new job every 10 minutes or a 4-hour job that does something every 10 minutes?"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow! Like we say in Cuba: el mango del arroz con mango! hahaha. I will dive into all this information as soon as I get home today. I wouldn't have a clue as to where to start looking/researching for all this, and all I was finding were very long complicated solutions. Thank you very much @mferryRV !

const util = require("util"); //to handle asynchronous functions

const exec = util.promisify(child_process.exec);

async function ping(hostname) {
try {
const { stdout, stderr } = await exec(`ping -c 3 ${hostname}`);
return { stderr, stdout };
} catch (err) {
return err;
}
}

ping("starter-kit-al84.onrender.com");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an environment variable