Not able to call C# function for CosmosDb Change feed from power shell

I am able to run the below code, if I run it as a console application, Now my requirement is that InstanceTime, ProcessorName and ChangeInstance is passed as an argument from PowerShell and run this method and get the required changed documents output. I created the cmdlet successfully but it doesn't give me any output, though it creates the changefeed instance in cosmos db, but there is no output regarding the changed documents in Power shell window. I would like to know what wrong am I doing here. I am not getting any error and I tried Logging Warning and Verbose but I dont get any thing. the code excutes without running anything but if run as a seperate console application, i get the desired docs.
My code:
{
[Cmdlet(VerbsCommon.Get, "ChangedRecording")]
[OutputType(typeof(Recording))]
public class SyncRecording : PSCmdlet
{
[Parameter(Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide the duration in days from when the changes in recording are seeked")]
[ArgumentCompleter(typeof(TwisterArgumentCompleter))]
public int DaysDuration { get; set; }
[Parameter(Mandatory = true,
Position = 1,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a Unique name for the Instance")]
[ArgumentCompleter(typeof(TwisterArgumentCompleter))]
public string changeFeedInstanceName { get; set; }
[Parameter(Mandatory = true,
Position = 2,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a Unique name for the ChangeProcessor")]
[ArgumentCompleter(typeof(TwisterArgumentCompleter))]
public string ChangeFeedProcessorName { get; set; }
protected override void ProcessRecord()
{
Task.Run(async () =>
{
DaysDuration = DaysDuration * -1;
DateTime particularPointInTime = DateTime.Now.AddDays(DaysDuration).ToUniversalTime();
CosmosClient Client = new CosmosClient("AccountEndpoint = https://test.documents.azure.com:443/;AccountKey=*********;");
if (DaysDuration == 0)
{
particularPointInTime = DateTime.Now.ToUniversalTime();
}
var database = Client.GetDatabase("testdatabase");
var container = database.GetContainer("testcontainer");
var leaseContainer = database.GetContainer("leases");
var cfp = container.GetChangeFeedProcessorBuilder<dynamic>(ChangeFeedProcessorName, ProcessChanges)
.WithLeaseContainer(leaseContainer)
.WithInstanceName(changeFeedInstanceName)
.WithStartTime(particularPointInTime)
.Build();
await cfp.StartAsync();
await Task.Delay(5000);
await cfp.StopAsync();
}).Wait();
}
public async Task ProcessChanges(IReadOnlyCollection<dynamic> docs, CancellationToken cancellationToken)
{
foreach (var doc in docs)
{
WriteObject(doc, true);
await Task.Delay(1000);
}
}
}
@Paulo Morgado Well nothing happens on removing Task.Delay(). I thought there is something to do with asynchronous and thats why tried different way of making this run. The above code also works fine if I just create a console application . It goes inside the delegate Processchanges() and read the changes . Just an FYI, I am following the code from here:
change-feed-processor
It's still not clear to me what you can achieve with a console application and not with the cmdlet and why do you think you need to create a cmdlet if a console application works.
It's also looking like you should be using the pull model instead of the push model.
You are moving in a different direction now. Yes the above code works if I create it as a console application but my business requirement is where I will be passing the arguments as powershell cmdlets. It will be a long story explaining why powershell, and I don't see that to be questioned here, as requirement differ with application.
The above model works and I don't understand the reason why it's not working with powershell cmdlets. It's a matter of passing the arguments and I am able to pass those arguments successfully but it does nt go to the delegate. There is something which I am missing.
figured out, error was task.delay() value to be less. task.delay(500000) gives the output. any suggestion how can I remove task.delay()? and the stopasync shouldn't be called unless the function has executed
Sign in to comment