Schedule the map-reduce script on the first working day of every month

The following method can be used to schedule the map-reduce script on the first working day of every month excluding Saturday and Sunday.

  • Schedule the map-reduce script for the 1st, 2nd, and 3rd of every month on the script deployment page.
  • Inside the map-reduce script, add the following function to identify whether the current day is 1st working day of the month or not.
            /**
             * Function used to check the current day is first working day of the month or not
             * @returns {boolean}
             */
            function checkFirstWorkingday() {
                try {
                    let date = new Date();
                    let dayOfWeek = date.getDay();
                    let dayOfMonth = date.getDate();

                    // Check if today is a first working day
           
                        if ((dayOfMonth == 1 && (dayOfWeek != 0 && dayOfWeek != 6)) || ((dayOfMonth == 2 || dayOfMonth == 3) && dayOfWeek === 1)) {
                            return true;
                        } else {
                            return false;
                        }
            

                } catch (e) {
                    log.error({
                        title: 'error @ checkFirstWorkingday',
                        details: e
                    });
                }
            }
  • Inside the getInputData of map-reduce, call the above function and check whether the current date is the first day of the month or not. And if the function returns true, the code inside the if condition will execute and functionality will work. Otherwise script will be failed.
  
     const getInputData = (inputContext) => {
            try {

                let executeScript = checkFirstWorkingday();

                if (executeScript) {
                    // code need to be execute
                }

            } catch (e) {
                log.error({
                    title: 'Error @ getInputData',
                    details: e
                });
            }
        }

Leave a comment

Your email address will not be published. Required fields are marked *