EncompDEV
CodeWizard

Exit Encompass at 4AM

Click here to download this plugin with source code

Why 4AM? It's the best time between most night people actually going to bed and most morning people actually waking up. Therefore it's the safest time to do anything with minimal user impact.

This plugin allows you to make sure that everyone's Encompass Client is in the same consistent state every morning. This is achieved by exiting Encompass at 4AM. If computer was in standby/hibernate at night, Encompass Client will exit as soon as computer wakes up. The following four major consistency issues are solved:

1. If you roll out Encompass version updates in the evening, everyone SmartClient will update from Ellie Mae next morning.
2. If you deploy global plugins, everyone will have the same set of plugins the next morning.
3. Inability of group policies to disable standby and users not understanding the need to exit Encompass for such updates.
4. Users accidentally leaving loans open & locked overnight preventing others from working early morning.

Thus this plugin will make admin updates significantly easier. Initially some users may complain that they loose their work. However they will get used to this within 2-3 days and will make sure their work is saved before leaving the office, which is a yet another bonus.

Code used for this plugin:
using System;
using System.Collections.Generic;
using System.Text;

using EllieMae.Encompass.ComponentModel;
using EllieMae.Encompass.Automation;
using EllieMae.Encompass.BusinessObjects;
using EllieMae.Encompass.BusinessObjects.Loans;

using System.IO;
using System.Timers;
using System.Diagnostics;

namespace ExitEncompassAt4AM
{
    [Plugin]
    public class EncompassPlugin
    {
        private Timer timer_exit = null;                    // timer to watch & restart
        private DateTime time_started = DateTime.MinValue;  // time when application started

        public EncompassPlugin()
        {
            EncompassApplication.Login += new EventHandler(EncompassApplication_Login);
        }

        // return TRUE if plugin should be activated
        private bool IsAllowed()
        {
            if (EncompassApplication.Session.UserID == "admin")
            {
                return false; // admin doesn't restart
            }
            else
            {
                return true; // enable for everyone else
            }
        }

        private void EncompassApplication_Login(object sender, EventArgs e)
        {
            if (IsAllowed() && timer_exit == null)
            {
                AddToLog("Starting Timer");
                try
                {
                    time_started = DateTime.Now;
                    timer_exit = new System.Timers.Timer(10000); // check every 10 seconds
                    timer_exit.Elapsed += new ElapsedEventHandler(OnTimer);
                    timer_exit.AutoReset = false;   // we restart timer manually
                    timer_exit.Enabled = true;      // start
                }
                catch (Exception ex)
                {
                    AddToLog("Error: " + ex.Message);
                }
            }
        }

        private void OnTimer(object sender, ElapsedEventArgs e)
        {
            // we want to restart when:
            // 1. time crosses 4AM
            // 2. system returned from standby/hibernation which crossed 4AM
            
            // time now
            DateTime dtNow = DateTime.Now;

            // cutoff time for today
            DateTime dtTodayCutoff = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 4, 0, 0);

            // AddToLog("Tick: Now=" + dtNow.ToString("yyyy-MM-dd HH:mm:ss") + 
            //    " Cutoff=" + dtTodayCutoff.ToString("yyyy-MM-dd HH:mm:ss"));

            // compare times against our 4AM cutoff for today
            if (time_started < dtTodayCutoff && dtNow >= dtTodayCutoff)
            {
                try
                {
                    Loan loan = EllieMae.Encompass.Automation.EncompassApplication.CurrentLoan;
                    if (loan != null)
                    {
                        AddToLog("Closing Loan " + loan.LoanNumber + " - " + loan.LoanName);
                        loan.Close();
                    }
                }
                catch (Exception ex)
                {
                    AddToLog("Error: " + ex.Message);
                }

                try
                {
                    AddToLog("Exiting Encompass");
                    Process p = Process.GetCurrentProcess();
                    p.Kill();
                }
                catch (Exception ex)
                {
                    AddToLog("Error: " + ex.Message);
                }
            }
            else
            {
                // start timer
                timer_exit.Enabled = true;
            }
        }

        public static void AddToLog(string s)
        {
            // put your logging code here if needed
            Debug.WriteLine("ExitEncompassAt4AM\t" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + s);  
        }
    }
}