ServiceStack RedisPubSubServer will enter in OnStop, OnInit, and OnStart frequently - servicestack

Here is my situation:
I instantiate a RedisPubSubServer and configure it with OnInit, OnStart, and OnStop callbacks.
I keep this reference as long as the application lives and, in the end, I dispose it.
Somehow, during the application life cycle, I noticed that the OnStart/OnStop callbacks are called without any explicit disposing of the RedisPubSubServer, and I suspect that something goes wrong with the connection, but I cannot figure why.
Is there any way of identifying the problem? Or am I am not understanding it right how it works?

If there's a connection error or failover the RedisPubSubServer will auto-reconnect, you can get notified when this happens by overriding the OnError and OnFailover callbacks.

Related

Cross domain trace listeners VB.NET

I have an application where I use TraceListener to collect data/debugging information while application executes. There are more than one listeners at the same time, one of them will write trace information to disk file, other may collect in string (for a short period of execution) and then it will be displayed to user or might saved to another file as per needs.
All went well, unless we decided something else.
We had lot of plugins which were loaded at runtime and executed. However, the issue with that was it was hard to replace the plugin DLL since it was used by application, so we had to stop and replace DLL and then start it again. We decided to use appDomain to load plugin into so that we can easily replace DLL without restarting the application(s).
That was also went well, but one major issue came in. the trace listerns were blind. Since the plugins are loaded into different app domain, so the traces never reached to the parent app domain which was listening to it (the traces made by plugin, which is loaded into new appdomain).
Our trace listener looks like this.
Public Class StringTraceListener
Inherits TextWriterTraceListener
Dim sw As System.Text.StringBuilder = Nothing
Public Overrides Sub WriteLine(ByVal message As String)
Try
MyBase.WriteLine(message)
sw.AppendLine(Now.ToString & " : " & message)
Catch ex As Exception
'Do not write anything here... or it might go into recursive loop
End Try
End Sub
I wonder, if there is a way that we don’t have to make much changes and the trace listener might receive traces from plugins (child appDomain) as well?
Any idea will be appreciated
Thanks
The config is read in when the application is loaded. So for a plug in where the assembly isn't loaded in the usual way, it is plausible that the config isn't being read. You can configure the trace listeners in code, although it is obviously less flexible.
To configure a trace listener in source code, add a listener to the TraceSource's Listener collection. Make sure this happens once for the life of that trace listener, else you will get duplicate Listeners.

How to dispose of a ViewModel in Durandal after Logout

I might be on the wrong track here, but here goes:
In my PhoneGap Durandal app, I have a Profile View/VM which only returns data the first time it is hit - after that it checks a bool called initialised and wont hit the DB again the 2nd time. This works fine.
However after Logout, I need to invalidate the cache. I could use a message to tell the Profile VM to clear the variable (ie. invalidate the cache) but I thought perhaps there is a higher-level way of doing this in Durandal - e.g. On Logout, I tell dispose of all ViewModels in memory (there may be other Singleton objects with session specific info in them).
Advice please...
This is more of a javascript question and this is just my understanding of how javascript works.
Javascript will automatically dispose of objects that are no longer referenced through a mechanism called Garbage Collection.
Here is a good article on how Garbage Collection works. Basically it will dispose of objects that are no longer referenced in your program.
There is another method in javascript that allows you to remove objects. The delete method:
delete someobj;
Which too my knowledge is pretty much equal to someobj = undefined;
Hope this helps.
***Edit
Durandal follows the screen activator pattern for it's viewmodels. So apart of the viewmodel lifecycle it will call an activate, candeactivate, and deactivate method.
You could do your disposing in the deactivate method.
(Durandal 2.0) You could always hook into the composition life-cycle callback methods on your view-model. There are four: activate(), attached(), deactivate(), and detached(). They are called automatically by Durandal on your view-model, if they exist. In my projects, if I need a view to invalidate its cache, I hook into the deactivate() method and put the cleanup logic there. Similarly, I use the detached() method to unbind events and destroy UI widgets.
Simple example:
define(['modules/myDataService'],
function(dataservice) {
var cache;
function activate() {
return dataservice.getData().done(function(response) {
cache = response;
});
}
function deactivate() {
cache = null;
}
return {
activate: activate,
deactivate: deactivate
};
});
Source documentation: http://durandaljs.com/documentation/Hooking-Lifecycle-Callbacks/

-shouldAutorotateToInterfaceOrientation related message

Here is something strange concerning AutorotateToInterfaceOrientation.
In the Debugger console I get this message for one of my view controllers :
The view controller returned NO from
-shouldAutorotateToInterfaceOrientation: for all interface orientations. It should support at least one orientation.
But in reality the rotations works perfectly well. And the message is wrong. Here is the code for -shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ([centerPoint autoRotateFlag]||(interfaceOrientation==centerPoint.userOrientation));
}
And either autoRotateFlag is simply true (YES), or if it is not centerPoint.userOrientation has been fixed to one of the four acceptable value.
This has been working for me for a long time and the app still works. I just don't know where this message is coming from.
Any idea?
By putting some tracing in my software, using NSLog; I realized that
shouldAutorotateToInterfaceOrientation was called seven times before
viewDidLoad was called.
Since my variable 'autoRotateFlag' is only initialized when passing through viewDidLoad. That explains my problem.
I have to admit though, that I was far from thinking shouldAutorotateToInterfaceOrientation could be called before viewDidLoad.
Obviously I was wrong. And I still do not fully understand the order in which all those methods are called.

Using CoWaitForMultipleHandles

Continuing from
Pausing execution of a Thread WITHOUT sleeping?
How do I use the CoWaitForMultipleHandles routine, as Lars Truijens suggested? I found the routine in the SyncObjs unit, however I get "undeclared identifier" when trying to call it? The IDE Insight does not bring anything up either? And yes, I have added SyncObjs to my Uses clause.
I can't see what other info I need to include - however feel more than free to ask for more info!
You can't call this function from SyncObjs, because it hasn't been declared in interface section. But TEvent.WaitFor actually calls CoWaitForMultipleHandles. Did you try it?
And please note its constructor declaration:
{ Specify UseCOMWait to ensure that when blocked waiting for the object
any STA COM calls back into this thread can be made. }
constructor Create(UseCOMWait: Boolean = False);

Calling QAxWidget method outside of the GUI thread

I'm beginning to wonder if this is impossible, but I thought I'd ask in case there's a clever way to get around the problems I'm having.
I have a Qt application that uses an ActiveX control. The control is held by a QAxWidget, and the QAxWidget itself is contained within another QWidget (I needed to add additional signals/slots to the widget, and I couldn't just subclass QAxWidget because the class doesn't permit that). When I need to interact with the ActiveX control, I call a method of the QWidget, which in turn calls the dynamicCall method of the QAxWidget in order to invoke the appropriate method of the ActiveX control. All of that is working fine.
However, one method of the ActiveX control takes several seconds to return. When I call this method, my entire GUI locks up for a few seconds until the method returns. This is undesirable. I'd like the ActiveX control to go off and do its processing by itself and come back to me when it's done without locking up the Qt GUI.
I've tried a few things without success:
Creating a new QThread and calling QAxWidget::dynamicCall from the new thread
Connecting a signal to the appropriate slot method of the QAxWidget and calling the method using signals/slots instead of using dynamicCall
Calling QAxWidget::dynamicCall using QtConcurrent::run
Nothing seems to affect the behavior. No matter how or where I use dynamicCall (or trigger the appropriate slot of the QAxWidget), the GUI locks until the ActiveX control completes its operation.
Is there any way to detach this ActiveX processing from the Qt GUI thread so that the GUI doesn't lock up while the ActiveX control is running a method? Is there something clever I can do with QAxBase or QAxObject to get my desired results?
After some experimentation, I was able to solve this by doing something I thought I'd tried earlier: creating a new QThread and calling QAxWidget::dynamicCall from the new thread. I must not have coded it correctly the first time I tried this solution; after sitting with a co-worker, we were able to get it to work. To be specific, what we did is:
(Step 1) Created a subclass of QThread to represent the thread I need to call dynamicCall().
(Step 2) In the constructor of my QThread, pass in a pointer to my original QAxWidget, and keep the pointer in a member variable.
MyThread::MyThread(QAxWidget* passedWidget) : QThread()
{
actualWidget = passedWidget;
}
(Step 3) In the run() method of the QThread, call the dynamicCall() method of the QAxWidget.
void MyThread::run()
{
QVariant result = actualWidget->dynamicCall(...parms as necessary...);
}
(Step 4) Back in my main code, when I need to execute dynamicCall(), I just call the start() method of MyThread. The start() method will execute run() in its own thread, thus sending the necessary command to the ActiveX object without blocking or stalling the main GUI thread.
If there is no event loop needed, then there is no need NOT to subclass QThread! I think this is the way to solve this without a bunch of signals to the main thread which (more than likely) owns the QAxWidget. The latest docs for Qt 5.3 referring to QThread also bears this out.

Resources