Jared Chu
Jared Chu

Reputation: 2852

Check Umbraco site is ready and it's connected to the database

My Umbraco site has a recurring task which runs every 60 mins. The problem is when Umbraco is not installed yet, the task block the installation process.

I'm trying to detect the status of my Umbraco site by this:

var isApplicationInstalled = uQuery.RootNodeId != -1;
if (isApplicationInstalled)
{
    // run the task
}

But uQuery.RootNodeId seems to always return -1 and the task has never run. How to detect that Umbraco site is installed and it's connected to the database?

Upvotes: 0

Views: 187

Answers (2)

Jared Chu
Jared Chu

Reputation: 2852

It's easier to check the Umbraco application's status via ApplicationContext:

ApplicationContext.Current.IsConfigured check Umbraco is configured. ApplicationContext.Current.DatabaseContext.CanConnect check Umbraco can connect to the database.

So the code will be:

    var isApplicationInstalled = ApplicationContext.Current.IsConfigured &&
                                    ApplicationContext.Current.DatabaseContext.CanConnect;
    if (isApplicationInstalled)
    {
        // run the task
    }

Upvotes: 1

Nhan Phan
Nhan Phan

Reputation: 1302

You can try this solution: Override ApplicationStarted method in ApplicationEventHandler.

The method is called when all required bootups is ready. Then you can override it, set a global setting to true (maybe you can define a global setting like UmbracoIsReady). And in your recurring task, you just need to retrieve UmbracoIsReady to check.

public class StartupHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
    {
        base.ApplicationStarted(umbracoApplication, applicationContext);

        //Set a global variable/information to make sure that the Umbraco is ready
        Setting.UmbracoIsReady = true;
    }
}

Upvotes: 1

Related Questions