Can I run long running task(synchronous) in OnStart() method in azure cloud service web role? - azure-web-roles

There is some initialization work (It takes around <= 10 min) which needs to be done before web role starts accepting the requests. SO my question is "Are there any constraints in OnStart() method"? Will the web role restart in between?

Are there any constraints in OnStart() method?
It seems there is not any constraint in OnStart() method. If the OnStart method returns false, the role instance is immediately stopped. If the method returns true, Windows Azure starts the role by calling the Run method. In general, you should avoid returning false from the OnStart method.
Will the web role restart in between?
If an exception occurs within one of the lifecycle methods, Azure will raise the UnhandledException event, and then the process is terminated. After your role has been taken offline, it will be restarted by Azure. So you should avoid UnhandledException in the method.
For more details, you could refer to the articles.
Customize the Lifecycle of a Web or Worker role in .NET
Role throws unhandled exceptions while initializing or stopping

Related

How to manually terminate a running Service Worker

I have developed a somewhat stateful Service Worker (which is admittedly not the best practice) and I have a need to manually terminate it in order to reset it.
The need arises when I am changing the user within my client application. As such I don't close the browser tab though I do however navigate outside of the Service Worker's scope (within the same site).
I cannot afford to wait the ~60 seconds or so before the Service Worker automatically terminates (having navigated outside of its scope or closed the client tab).
I cannot close the client browser tab (this is not a good user experience in my scenario).
I am not looking to uninstall/unregister the service worker.
It seems like the ServiceWorkerGlobalScope does not inherit the close() method from WorkerGlobalScope.
It seems like navigator.serviceWorker does not inherit terminate() method from WebWorker.
How can I manually stop the Service Worker from running and to clear its state?
you can terminate a service worker with this:
navigator.serviceWorker
.getRegistration('/miSwScope')
.then(function(registration) {
if (registration) {
registration.unregister();
}
});
or clear all:
navigator.serviceWorker.getRegistrations().then(function(regs) {
regs.forEach(reg => {
reg.unregister();
});
});
but to clear state and init state again you need implement your own logic depending of your app.

Configuring Web Logic JMS Module with Oracle AQ Queues

I testing setting up a web logic server which can use an Oracle AQ JMS implementation, but for some reason my WLS connection pool cannot see the queue I have created, the only way I can see the find the queue in web logic is to make the web logic datasource connection pool use the credentials of the DB user who created the queue.
I have done the following as admin_user:
Created a Queue Table
EXECUTE DBMS_AQADM.CREATE_QUEUE_TABLE(queue_table=>'testqueue_table',queue_payload_type=>'sys.aq$_jms_text_message');
Created a Queue
EXECUTE dbms_aqadm.create_queue(queue_name=>'testqueue', queue_table=>'testqueue_table');
Started the Queue
EXECUTE dbms_aqadm.start_queue(queue_name=>'testqueue');
Granted the CPOOL user permissions
GRANT aq_user_role TO cpool;
EXECUTE DBMS_AQADM.grant_queue_privilege(privilege=>'ALL', queue_name=>'adming_user.testqueue', grantee=>'cpool',grant_option=>FALSE);
Set up 2 JDBC DataSources in Web Logic
I set up 2 datasources in web logic which connect to my database, both are identical except that one uses the cpool user and the other uses admin_user
Create JMS Modules
Now I register a jms module with the JDBC DataSources and wire up the local and foreign JDNI names.
If I use the DataSource which uses admin_user everything works fine and my deployed application can find queue in JNDI.
Problem
If I use the data source which uses cpool then it fails finds the connection pool in JNDI but never the Queue (despite the fact it worked for admin_user). I get the following error from WL:
Related cause:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testqueue': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Cannot resolve name:Queues/TESTQUEUE
Does anyone know why WL cannot resolve the JNDI name for the queue unless I use the admin_user account for my datasource.
The answer was that my connection pool user was missing some grants, the following where required:
GRANT EXECUTE on DBMS_AQ to <CPOOL>;
GRANT EXECUTE on DBMS_AQADM to <CPOOL>;
GRANT aq_user_role to <CPOOL>;
Make sure the user who creates the destination grants access to the connection pool user:
`EXECUTE DBMS_AQADM.grant_queue_privilege(privilege=>'ALL', queue_name=>'<SCHEMAOWNER>.docprod_queue', grantee=>'<CPOOL>',grant_option=>FALSE);`
Finally in Web Logic make sure to reference the destination by their full foreign JNDI name:
Queues/<SCHEMA OWNER>.<DESTINATION_NAME>

What starts first Application_Start or WebRole's OnStart?

I have MVC project on Windows Azure. This project has WebRole with method OnStart. And I want to know what starts first Application_Start of Global.asax or WebRole's OnStart?
Pretty sure this is non-deteministic after SDK 1.3. Trying to time something from one to the other will just end in a race condition.
Prior to 1.3, the Hosted Worker Core (HWC) was hosted in the RoleEntryPoint, so there was a deterministic startup (OnStart, Application_OnStart, IIRC). However, with IIS hosting the web now, it is the IISConfigurator.exe that creates the app pool, etc, and eventually w3wp.exe hosts your web site. This is a different process than your RoleEntryPoint, so either one could start first.
I can't find any 100% complete references on this (about the best I've seen is http://blog.syntaxc4.net/?p=271), but I believe the sequence is:
Any startup tasks are run
WebRole OnStart method is called
The WebRole Run() method is called - and at the same time IIS starts the websites - so at this point Application_Start will get called for the first time.
According to Microsoft's documentation, the Application_Start method is called after the OnStart method.

StructureMap fails on IIS 7.5 with AppPoolIdentity

I have a problem with ASP.NET MVC application hosted on IIS 7.5 on separate application pool, configured with AppPoolIdentity and .NET 4.0 Framework.
The application is working normally for some time and suddenly part of controllers start to throw an exception inside StructureMap:
System.ArgumentNullException: Trying
to find an Instance of type [MyType]
Parameter name: instance
in
StructureMap.InstanceCache.Get(Type
pluginType, Instance instance) +376
The type requested was registered correctly (and was resolved properly just a minute before the exception occured). I can't verify it is still registered, as WhatDoIHave throws an exception, too:
NullReferenceException: Object
reference not set to an instance of an
object. in
StructureMap.Diagnostics.WhatDoIHaveWriter.writeInstance(InstanceRef
instance) +133
Looks like StructureMap's internal cache gets corrupted somehow.
The problem disappears when I change AppPoolIdentity to "real" local system user credentials. So it looks like the problem is somewhere between IIS and StructureMap. Maybe app pool recycling makes problems here? But it is configured to recycle every 1740 minutes and it fails sometimes after 5 minutes from its start...
Well, the problem appeared to be very different, I don't know how it may looked like connected to app pools and privileges.
The flaw is in GetNamedInstance<T> StructureMap's container method. I requesting a named instance, which is not registered, StructureMap tries to create the instance and add it to the container. Creation fails, but nevertheless, StructureMap registers null in the container. Each subsequent request for that named instance fails when encounter this null.
Our workaround is that we use GetAllInstances<T> instead of GetNamedInstance<T> and iterate manually through all instances of T to find matching one.

How can I start a browser from a windows service

I need to create a windows service that when launced open a specific URL.
What I did is to override the onStart() method by adding the following lines :
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Browser must start " + DateTime.Now);
string targetURL = "http://www.mysite.com";
System.Diagnostics.Process.Start(targetURL);
}
However this thing doesn`t work . :((
Thing is that it does write the log .than means that onStart Anybody has any ideas????
The service is usually started (when it's in Automatic startup mode) when there's no user logged in.
In general, services don't interact with user desktop and work in a separate session. If you need something to be performed for each or some of logged in users, you need to write a separate agent application, which will be automatically started on user login, and with which your service will communicate. Then the agent can start the browser or do whatever else you need.
Simple answer is, if you're using Vista or later you can't. This is due to session 0 isolation. To quote from the document linked in that page:
For more complex interactions,
developers should move their UI code
into an agent that runs in the user’s
session and handles all UI
requirements. The agent communicates
with the service through RPC or named
pipes.
Windows services don't have a GUI. What you can do is create a controller that interacts with your service and have it launch a web browser.
This link doesn't directly answer your question but contains enough links in the answers to put you on the right path: How can I run a Windows GUI application on as a service?

Resources