Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scheduled Restart on CloudHub

DZone's Guide to

Scheduled Restart on CloudHub

Have you ever been required to restart applications in CloudHub at a specific time of the day on weekdays? Automate it with CloudHub API and make life easier.

· Cloud Zone
Free Resource

What if you could learn how to use MongoDB directly from the experts, on your schedule, for free? We've put together the ultimate guide for learning MongoDB. Sign up and you'll receive instructions for how to get started!

Have you ever been required to restart applications in CloudHub at a specific time of the day on weekdays? For example, in a testing environment when the app should be restarted at the beginning of the day before the testers commence their duties. This might sound simple as it's just few clicks to restart an application in CloudHub.

But...

What if you have a different shift and you're unable to do it on time?

What if one day you forgot to do it?

What if there are too many applications to restart?

The solution is to make this thing automated, but how? No problem, we can do it by utilizing CloudHub API and a time-based job scheduler.

What do we need?

  • Access to CloudHub API service: https://anypoint.mulesoft.com/cloudhub/api/applications/{domain}/status

  • X-ANYPNT-ENV-ID: The environment where the application deployed example: 6ebdb30c-b6bf-4d75-b8ba-61cb87b5ed4b

  • Domain - The application domain example: cloudhub-restarter

  • Anypoint Platform for API account with 'Manage Settings' role.

  • A Time-based job scheduler example: CRON, Mulesoft Poll Scheduler

After we got all of the things we need, let's create the application in Mule with a scheduled restart at 6:00 AM every weekdays.

This simple flow used Poll Scheduler to configure a polling schedule using cron expressions. When the given schedule is attained it will invoke the CloudHub REST API to restart the application using the defined values in the properties.

Image title

Put the environment id, username, and password in mule-app.properties. Replace {domain} with the application domain you want to be restarted.

Image title

Here's the Mule configuration XML.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:schedulers="http://www.mulesoft.org/schema/mule/schedulers"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/schedulers http://www.mulesoft.org/schema/mule/schedulers/current/mule-schedulers.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:request-config name="HTTP_Request_Configuration"
protocol="HTTPS" host="${cloudhub.restapi.host}" port="${cloudhub.restapi.port}"
basePath="${cloudhub.restapi.base.path}" doc:name="HTTP Request Configuration">
<http:basic-authentication username="${anypoint.platform.account.username}"
password="${anypoint.platform.account.password}" />
</http:request-config>
<flow name="cloudhub-restarterFlow">
<poll doc:name="Poll">
<schedulers:cron-scheduler expression="${cloudhub.restart.schedule}" />
<flow-ref name="invoke-restart-Flow" doc:name="invoke-restart-Flow" />
</poll>
<logger message="The Application has been restarted." level="INFO"
doc:name="Logger" />
</flow>
<flow name="invoke-restart-Flow">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"status":"restart"
}]]></dw:set-payload>
</dw:transform-message>
<http:request config-ref="HTTP_Request_Configuration"
path="${cloudhub.restapi.application.status.endpoint}" method="POST"
doc:name="HTTP">
<http:request-builder>
<http:header headerName="X-ANYPNT-ENV-ID"
value="${anypoint.platform.environment.id}" />
</http:request-builder>
</http:request>
</flow>
</mule>

See how simple, fast, and easy CloudHub RESTful API can be used, if we explore more there is more than just restarting that we can do with the help of this API. See the full API reference.

Please leave a comment :)

What if you could learn how to use MongoDB directly from the experts, on your schedule, for free? We've put together the ultimate guide for learning MongoDBSign up and you'll receive instructions for how to get started!

Topics:
mulesoft ,cloudhub ,rest api

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}