cron/index.js
// adds a new cron job and watches resource status
adds a new cron job and watches resource status
adds a new cron job and watches resource status
function addCronJob(key, url, resource_id, checkStatus, time = "*/30 * * * *") {
  checkStatus(url, resource_id);
  manager.add(
    key,
    time,
    async () => {
      try {
        const res = await axios.get(url);
        if (res.status === 200) {
          console.log(`${url} is up and running`);
          const resource = await Resource.update(
            { status: true, last_check: moment().format() },
            resource_id
          );
          await Log.add({
            id: uuidv4(),
            last_check: resource.last_check,
            code: res.status,
            status: resource.status,
            resource_id: resource.id,
          });
        }
      } catch (error) {
        const resource = await Resource.update(
          { status: false, last_check: moment().format() },
          resource_id
        );
        await Log.add({
          id: uuidv4(),
          last_check: resource.last_check,
          code: error.response.status,
          status: resource.status,
          resource_id: resource.id,
        });
        const user = await User.findByFilter({
          id: resource.user_id,
        });
        sendMail(
          user.email,
          `${user.name}, resource ${resource.name} at ${resource.link} is down!`
        );
        console.log(`${user.name}, ${url} is down`);
      }
    },
    { start: true }
  );
}
function addCronJob ( key , url , resource_id , checkStatus , time = "*/30 * * * *" ) { checkStatus ( url , resource_id ) ; manager . add ( key , time , async ( ) => { try { const res = await axios . get ( url ) ; if ( res . status === 200 ) { console . log ( ` ${ url } ` ) ; const resource = await Resource . update ( { status : true , last_check : moment ( ) . format ( ) } , resource_id ) ; await Log . add ( { id : uuidv4 ( ) , last_check : resource . last_check , code : res . status , status : resource . status , resource_id : resource . id , } ) ; } } catch ( error ) { const resource = await Resource . update ( { status : false , last_check : moment ( ) . format ( ) } , resource_id ) ; await Log . add ( { id : uuidv4 ( ) , last_check : resource . last_check , code : error . response . status , status : resource . status , resource_id : resource . id , } ) ; const user = await User . findByFilter ( { id : resource . user_id , } ) ; sendMail ( user . email , ` ${ user . name } ${ resource . name } ${ resource . link } ` ) ; console . log ( ` ${ user . name } ${ url } ` ) ; } } , { start : true } ) ; }
adds a new cron job and watches resource status
adds a new cron job and watches resource status
// update resource status to up/true // add log record of the status check // console.log(error); // update resource status to down/false // add log record of the status check // send a text/email notification to the user that their resource is down // grab user name and email and pass to email function