I will try to run a scheduled task on Sharperlight instance B from Sharperlight instance A.
Create a custom report using the Sharperlight publisher and execute a pre-defined Sharperlight scheduled task on another Sharperlight instance.
The user token for the Admin user to be used is prepared in advance at Sharperlight instance B side, where the target scheduled task is on.
Prepare the HTML code for the custom report
Create a custom report here. Create a little script and a button with your text editor.
Now let's write the HTML code.
This is the header part. Since JQuery commands are used in the script part below, the JQuery library is referenced. Sharperlight comes with several JavaScript libraries, which can be referenced by such descriptions.
The rest is a CSS definition for decorating the button.
I will introduce the Body part by dividing it into a script part and an HTML tag part.
Let's start with the script part.
Here we use JQuery's ajax command to post the task execution request.
You should see three global variables. One uses the control tag {*Global.Value.AdminToken}
to extract the value of the token stored in the Sharperlight system property beforehand. AdminToken
is the code name of the system property that stores the token.
The name of the task to be executed is defined as the query output item of the report that is the container for this HTML code, and is obtained with the control tag {*Row.TaskName.text}
.
The service URL of Sharperlight instance B, where the task to be executed is defined, is also defined in the system property and extracted with the control tag {*Global.Value.TargetService}
.
The HTML tag in the body is a simple button layout like this.
Preparing a report to be a container for custom code
Create a published report that will be a container to hold the HTML code you created earlier.
Every published report must have a query defined.
This time, we don't need to define a query to display the data, but we must define a query. Therefore, I define the task name with the query and pass it to the custom HTML code at runtime. By doing so, even if the task you want to execute changes, you can respond by simply changing the query instead of editing the HTML code.
Creating a new published report
Start Sharperlight Publisher from Sharperlight Application Menu.
Once the publisher window opens, start creating a new published report with the New
button.
With the published report dialog, fill in Code, Group and Title.
Creating query
Start editing the query with the Edit Query
button.
When Query Builder starts, set Product
to System, Table
to User Accounts, and Mode
to CU as shown below. With this setting, the query will return only one record.
In this case, we just want to pass the name of the task we want to run to the HTML code, so one record is enough.
Now, use the expression to define the name of the task you want to execute as an output item.
Select Add Expression
from the right-click menu in the output area.
The editor will start, so define it as shown below. Save with the OK button.
After saving, change the name to an easy-to-understand name as shown below. The name enclosed in a red frame is the code for reference.
Validate your query with the preview feature. If you get a result like this, you're done. Save the query with the OK button.
Embedding HTML code
After saving the query and returning to the Publish Report dialog, it's time to embed the HTML code we prepared earlier.
Go to Options tab. Use the custom HTML option on this tab.
Tick the checkbox and launch the editor with the Edit
button.
Paste the HTML code prepared earlier into the text field of the editor. Save with the OK button.
This completes the creation of the published report. Save with the OK button.
Verification
Start the Sharperlight service.
Paste the link address of the published report you created into your browser's address bar.
QA1Rest
is the service code of the Sharperlight instance where this published report resides.
http://localhost/QA1Rest/Report/?query=QiitaSample.TriggerTaskVisible
You will see a simple page with just one button like this.
Click the button. the target task on the specified Sharperlight service should have run in the background. Let's confirm it.
Start the scheduler of the Sharperlight instance where the specified task is defined and open the desired task.
Go to the history tab and click the delete icon to clear all history.
Go back to your browser and click the button. Immediately return to the task history tab and click the refresh icon.
You can check the history of what has just been executed.
However, there is one security issue here.
When you view the source code with your browser's debug tool, the user token is completely visible.
I'll cover how to deal with this in another article, so that's all for now.
【Publisher】Thinking about invisible ways of tokens