Hello Together. I did write a JS to GET fetch orders from an external API and then creates Deals, LineItems and Contacts accordingly in HubSpot. I did use Axios for it. That script has to run every couple minutes. It all runs fine locally.
Then I tried to find a way to run that on DigitalOcean. First I did an Function then an App based on that tutorial: https://www.howtogeek.com/devops/how-to-get-started-with-digitaloceans-serverless-functions/. What puzzles me is, if every script has to be like these schema, but it seems:
function main(args) {
return {
body: {
value: (args.value * 2),
timestamp: Date.now()
},
headers: {
"Content-Type": "application/json"
}
};
}
console.log("Blub")
How Do I implement includes and a script in that. Something like that I did for testing purposes:
const nodemailer = require('nodemailer');
var transport = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: "xxx",
pass: "xxx"
}
});
transport.sendMail({
from: 'sender@example.com',
to: 'me@example.com',
subject: 'Message',
text: 'I hope this message gets delivered!'
}, (err, info) => {
console.log(info.envelope);
console.log(info.messageId);
});
After that I did create a Dropplet and with that everything works as I can install there AXIOS. I haven’t tried the CronJobs, but I think it would work. Is that the way to go? But then I think I shomehow should link it to my GIT to push the changes automated over there?
Thank You; Thomas.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
It sounds like you have a use case that involves running code on a schedule and you’ve tried a bunch of our services to run it. Based on my understanding, I do think Functions would be appropriate to run your code. And, because it’s the most serverless of our product offerings, it’s probably the best option for you to minimize operations work over time.
By “includes”, I’m assuming you’re referring to importing code in Node.js using
require
.To deploy a Node.js function that uses 3rd party libraries like
axios
, you would include apackage.json
file in your function directory in your project. You can find an example of such a project at https://github.com/digitalocean/sample-functions-nodejs-qrcode. Note how there is apackage.json
file where the libraries are listed underdependencies
.As you’ve already noted, the function must follow a particular schema:
So, you need to put your business logic into that function body. You mentioned using
axios
and you include a code example here usingnodemailer
. Those are examples of the business logic I’m referring to. The example I linked above shows this concept too. There, the business logic is put into the function body. Because the sample uses apackage.json
file, turning the function directory into an NPM module,exports
is used instead of defining the function at the top level. Currently, it looks like this:Note how the
require
is outside of the function body. This is an optimization. The code inside the function body has to run every time the function is invoked. But, the code outside the function body only has to run during cold starts. It makes sense to do setup stuff outside of the function body to minimize function execution time for warm functions.Once you’ve deployed your function, have a look at Scheduled Functions (https://docs.digitalocean.com/products/functions/how-to/schedule-functions/), currently in beta. It allows you to have DigitalOcean automatically invoke your function on a cron schedule. You’ll set up a “trigger”. You can use websites like crontab.guru to easily create the cron schedule for your use case.