Sunday, December 15, 2019

Delete files older than x days on Azure Web App



Get files older than 30 days.
forfiles -s -m *.* -d -30 -c "cmd /c echo @file" (All files)


Once results look ok, change echo to delete. 
forfiles -s -m *.* -d -30 -c "cmd /c del @file"

Change *.* to * if you want to include directories. 

to have a condition


forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==TRUE echo @file is a directory"

Remove all directories from the current directory older than 3 days.

forfiles -s -m * -d -3 -c "cmd /c if @isdir==TRUE RMDIR /S /Q @file"


Tuesday, October 15, 2019

HangFire Ioc Contaner - Job Schedular

Nuget :
Hangfire.AspNetCore
Hangfire.SqlServer (Redis can also be used)

services.AddHangfire(x => {
                var options = new SqlServerStorageOptions
                {
                    PrepareSchemaIfNecessary = true,
                    QueuePollInterval = TimeSpan.FromSeconds(5)
                };
                x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"), options);
                });


 //services.AddHangfire(configuration => {
            //    configuration.UseStorage(
            //        new MySqlStorage(
            //            "server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True",
            //            new MySqlStorageOptions
            //            {
            //                TablesPrefix = "Hangfire"
            //            }
            //        )
            //    );
            //};


//https://docs.hangfire.io/en/latest/configuration/using-sql-server.html
            //https://docs.hangfire.io/en/latest/configuration/
            //https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html
            GlobalConfiguration.Configuration.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"));
            GlobalConfiguration.Configuration.UseActivator(new ServiceJobActivator(serviceProvider.GetService<IServiceScopeFactory>(), serviceProvider));
            //GlobalConfiguration.Configuration.UseActivator(new HangfireActivator(serviceProvider));
            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            { 
                
            });
            app.UseHangfireServer(new BackgroundJobServerOptions { 
                 WorkerCount= 1
            });

Job Activator
public class ServiceJobActivator : JobActivator
    {
        readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly IServiceProvider _serviceProvider;

        public ServiceJobActivator(IServiceScopeFactory serviceScopeFactory, IServiceProvider serviceProvider)
        {
            if (serviceScopeFactory == null) throw new ArgumentNullException(nameof(serviceScopeFactory));
            _serviceScopeFactory = serviceScopeFactory;
            _serviceProvider = serviceProvider;
        }

        public override JobActivatorScope BeginScope(JobActivatorContext context)
        {
            return new ServiceJobActivatorScope(_serviceScopeFactory.CreateScope());
        }

        public override object ActivateJob(Type type)
        {
            return _serviceProvider.GetService(type);
        }
    }


public class ServiceJobActivatorScope : JobActivatorScope
    {
        readonly IServiceScope _serviceScope;
        public ServiceJobActivatorScope(IServiceScope serviceScope)
        {
            if (serviceScope == null) throw new ArgumentNullException(nameof(serviceScope));
            _serviceScope = serviceScope;
        }
        public override object Resolve(Type type)
        {
            return _serviceScope.ServiceProvider.GetService(type);
        }
    }


In the service, inject IBackgroundJobClient backgroundJobs and use 
_backgroundJobs.Enqueue<ISampleService>(s => s.CallFunction());




Monday, August 19, 2019

Create Docker container with the image less than 90 days old

Please add -accept_eula in the command to create the container.

Sample Command

New-NavContainer -containerName NAV2017 -accept_eula -accept_outdated -alwaysPull -auth Windows -authenticationEMail test@bizcentralus.onmicrosoft.com -imageName mcr.microsoft.com/dynamicsnav:2017-cu3-na -includeCSide -licenseFile "C:\ABC.flf" -useSSL 

Monday, August 12, 2019

Batch Upload to Azure Storage using Azure CLI

az storage blob upload-batch -d <container_name> -s '<directory_path>' --pattern *.*

  --account-name <account_name>
  --account-key <account_key>

Wednesday, July 24, 2019

Copy Docker Image from One machine to another

go to the machine that has image. run following command. You need to use repository name and tag.  if you don't use them but use image id, that tag and repository won't carry over to the new image.

docker save microsoft/bcsandbox:us > c:\newd365.tar

Once done, copy the file to the new machine and import it using following command.

docker load -i c:\newd365.tar

It may take a while and then you should get the image.

list the images using
docker image ls -a

and you should see the image. 

Monday, July 15, 2019

Getting and Killing Process running at certain port


Get all the processes running at port using

netstat -ano | findstr :yourPortNumber

and then
kill using pid
taskkill /pid {processid} /f

Sunday, March 31, 2019

Configure Redis in Development Environment


  1. Run this on Command Prompt.(Command Prompt with Admin rights)
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
  1. Now Run “choco install redis-64”
  2. Once this done execute “redis-server”. This will start redis server
and then can access redis cache by using the connection string "localhost:6379,ssl=false"
Helpful links:



For GUI
npm install -g redis-commander
redis-commander

Thursday, March 21, 2019

Analize Angular Bundle Size

Install Analyzer
npm i -D webpack-bundle-analyzer

Run Prod build command with --stats-jso argument. 

This will create stats.json. 

then run analyzer command to analyze the result. 
webpack-bundle-analyzer ./dist/stats.json

If any issue running command then add following to your package.json and run npm run bundle-report.


"bundle-report": "webpack-bundle-analyzer dist/stats.json"

Wednesday, February 27, 2019

.net standard project doesnt copy nuget referenced file to bin

add following tag (CopyLocalLockFileAssemblies) into your project file in order for it to work.

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>