-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Schedule cron job in loopback #4047
Comments
To solve this I've used a workaround: I've created a public endpoint and a cron job that call that endpoint using https://cron-job.org/ |
@anandrajputSF can you explain your requirements in detail? |
@hacksparrow basically my requirement is connect with sql server, after some time interval fetch data from tables and log some where, if next time cron run, it will pick data from log table last run time and from this time it will again fetch data from sql server |
@anandrajputSF LoopBack does not provide an in-built solution to your specific usecase, however with Remote hooks it can be very well achieved. |
@anandrajputSF were you able to solve your problem? |
@hacksparrow i haven't try. |
@anandrajputSF I have implemented something similar in loopback, you can do it the way you would do it in NodeJS and its nothing specific to loopback. `'use strict'; const CronJob = require('cron').CronJob; module.exports = function (app) {
}` Hope this Helps ! |
@nikhil-g777 This seems to be a great solution if the application is running on a single instance. But if I have more instances running the same time they will run the same cron job multiple times. |
@matteo-pennisi
This is more like a workaround, but I believe it should solve the problem |
It would be awesome if LB would have a way to inject a component, as a service or repository, in a cron component. Also invoke a self rest endpoint. |
This solution has worked for me. Installation
Create a component
Using Loopback DI Container to get a controller instance:
|
@dtelaroli Does that mean that the cron is only run on one instance? |
@filipef101 you can have multiple instances into the CronController or multiples Controllers into the Cron. It's up to you, but I think that is easier into the CronController, otherwise you should inject manually multiple controllers. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
Just for reference, there is a package called Bull that allows assigning an ID to the Job. If running multiple instances, attempting to create a job with an ID that already is in the queue will be ignored. |
This solution worked for me as well. Loopback 4 tested. Example in repo: https://github.com/tangoat2019/covid19-tracking-api |
How to create schedule cron job in loopback, where can I code? I tried to implement
node-schedule
but not work. I don't know how to call my controller code?The text was updated successfully, but these errors were encountered: