Code Optimization

Interesting things about software development and code optimization

Dropbox as a service (Windows Service for Windows Server)

Hello friends,


Have you searched for a Dropbox windows service so it would synchronize your files and folders on a specific time and without any logged in windows user? I guess yes and as me it seems that you have not found anything appropriate.

The question is not only about synchronization if we talk about windows server but about performance as well. As you know the Dropbox service use a lot of system resources like CPU, HDD/SSD, RAM while it is running and this is crucial for a server because of any server has to use as less resources as possible at any given time.


So that is why I implemented my own workflow to control dropbox. There are two main points:

- run non-GUI Dropbox instance on a specific time frame only

- if there is any dropbox instance outside of the specified timeframe then kill it immediately.


To run the dropbox without GUI and to start synchronizing files I use the following windows task scheduler:





And here is the windows service we will install and run indefinitely to monitor any dropbox process and kill it:

    public partial class Service1 : ServiceBase
    {
        System.Threading.Timer timer = null;
        System.Threading.Thread tt = null;
        volatile bool stop = false;
        var perod = new Setting();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            perod.StartFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 25, 0);
            perod.EndTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 25, 0).AddHours(1);
            
            tt = new System.Threading.Thread((Action) =>
            {
                if (timer == null)
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(KillDropbox), this, 5 * 1000, 60 * 1000);

                while (!stop)
                {
                    System.Threading.Thread.Sleep(3000);
                }
            });
            tt.Start();
        }

        void KillDropbox(object o)
        {
            try
            {
                if (DateTime.Now.TimeOfDay >= perod.StartFrom.TimeOfDay && DateTime.Now.TimeOfDay <= perod.EndTo.TimeOfDay)
                    return;

                int pc = 0;
                ReScan:
                var proccs = System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName.ToLowerInvariant().StartsWith("dropbox"));
                pc = proccs.Count();
                foreach (var proc in proccs)
                {
                    try
                    {
                        proc.Kill();
                        
                        System.Threading.Thread.Sleep(1000);
                        goto ReScan;
                    }
                    catch (Exception eex)
                    {
                    }
                }
            }
            catch(Exception ex)
            {
            }
            
        }

        protected override void OnStop()
        {
            stop = true;

            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
            if (tt != null)
            {
                tt.Join(5000);
                tt = null;
            }
        }
    }


So after all we have a scheduler that runs non-GUI dropbox, dropbox synchronizes all configured files and folders and our windows service kills it outside of time frame.


Note: You should select your own time frame that will give dropbox enough time to synchronize everything.


It is also worth to mention that you will have to login to your server from time to time and update and sign into your Dropbox application in other way it will stop synchronization (something special that relates to dropbox itself).


Thank you, hope that was helpful!


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



NopCommerce customization - Price Calculation Service

Hello friends,

today I will show a Price Calculation Service implementation for NopCommerce plugin.

We will need a class that inherits Nop.Services.Catalog.PriceCalculationService and single method for price calculation.

Here is the code:

    public class MyPriceCalc:Nop.Services.Catalog.PriceCalculationService
    {
        #region Fields

        private readonly CatalogSettings _catalogSettings;
        private readonly CurrencySettings _currencySettings;
        private readonly ICategoryService _categoryService;
        private readonly ICurrencyService _currencyService;
        private readonly IDiscountService _discountService;
        private readonly IManufacturerService _manufacturerService;
        private readonly IProductAttributeParser _productAttributeParser;
        private readonly IProductService _productService;
        private readonly IStaticCacheManager _cacheManager;
        private readonly IStoreContext _storeContext;
        private readonly IWorkContext _workContext;
        private readonly ShoppingCartSettings _shoppingCartSettings;

        private readonly IGenericAttributeService _genericAttributeService;
        private readonly Nop.Services.Shipping.Date.IDateRangeService _dateRangeService;

        #endregion

        #region Ctor

        public MyPriceCalc(CatalogSettings catalogSettings,
            CurrencySettings currencySettings,
            ICategoryService categoryService,
            ICurrencyService currencyService,
            IDiscountService discountService,
            IManufacturerService manufacturerService,
            IProductAttributeParser productAttributeParser,
            IProductService productService,
            IStaticCacheManager cacheManager,
            IStoreContext storeContext,
            IWorkContext workContext,
            ShoppingCartSettings shoppingCartSettings,
            IGenericAttributeService genericAttributeService,
            Nop.Services.Shipping.Date.IDateRangeService dateRangeService) :base(catalogSettings,
            currencySettings,
            categoryService,
            currencyService,
            discountService,
            manufacturerService,
            productAttributeParser,
            productService,
            cacheManager,
            storeContext,
            workContext,
            shoppingCartSettings)
        {
            this._catalogSettings = catalogSettings;
            this._currencySettings = currencySettings;
            this._categoryService = categoryService;
            this._currencyService = currencyService;
            this._discountService = discountService;
            this._manufacturerService = manufacturerService;
            this._productAttributeParser = productAttributeParser;
            this._productService = productService;
            this._cacheManager = cacheManager;
            this._storeContext = storeContext;
            this._workContext = workContext;
            this._shoppingCartSettings = shoppingCartSettings;

            this._genericAttributeService = genericAttributeService;
            this._dateRangeService = dateRangeService;
        }

        #endregion

        public override decimal GetFinalPrice(Product product,
            Customer customer,
            decimal? overriddenProductPrice,
            decimal additionalCharge,
            bool includeDiscounts,
            int quantity,
            DateTime? rentalStartDate,
            DateTime? rentalEndDate,
            out decimal discountAmount,
            out List<DiscountForCaching> appliedDiscounts)
        {
            //get base price in case anything will go wrong in your logic
            decimal fprice = base.GetFinalPrice(product,
            customer,
            overriddenProductPrice,
            additionalCharge,
            includeDiscounts,
            quantity,
            rentalStartDate,
            rentalEndDate,
            out discountAmount,
            out appliedDiscounts);

            if (_storeContext.CurrentStore.Id == 3/*your store id in multiple-store configuration*/)
            {
                try
                {
                    //... your price calculation logic
                }
                catch (Exception ex)
                {
                    var logger = EngineContext.Current.Resolve<Nop.Services.Logging.ILogger>();
                    logger.Error(ex.Message, ex, customer);
                }
            }

            return fprice;
        }
    }

So, this class and the single method will be called every time any product requested (on home page, in any list, on product details page, etc.)

Firstly I call base method just in case anything will go wrong in my own logic and return the base price.

After you published this plugin your calculation class will picked up by NopCommerce immediately.


Thank you and see you there: NopCommerce customization - Events




1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y