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

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.


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: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}" />
<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" />
<logger message="The Application has been restarted." level="INFO"
doc:name="Logger" />
<flow name="invoke-restart-Flow">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
<http:request config-ref="HTTP_Request_Configuration"
path="${cloudhub.restapi.application.status.endpoint}" method="POST"
<http:header headerName="X-ANYPNT-ENV-ID"
value="${anypoint.platform.environment.id}" />

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 :)

cloudhub, mulesoft, rest api

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}