Scheduled Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for a scheduled worker.

export default async (
 context: PlatformContext,
 data: ScheduledEventRequest
): Promise<ScheduledEventResponse> => {
 try {
   // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
   //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
   const res = await context.clients.platformHttp.get(
     "/artifactory/api/v1/system/readiness"
   );


   // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
   if (res.status === 200) {
     console.log("Artifactory ping success");
   } else {
     console.warn(
       `Request is successful but returned status other than 200. Status code : ${res.status}`
     );
   }
 } catch (error) {
   // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
   console.error(
     `Request failed with status code ${
       error.status || "<none>"
     } caused by : ${error.message}`
   );
 }


 return {
   message: "Overwritten by worker-service if an error occurs.",
 };
};

Input Parameters

Context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).PlatformContext

Data

The trigger ID of the scheduled execution

{
   "triggerID": "triggerID"
}
Response
{
   message: 'custom message', // Message to print to the log. If an error occurs, it will be printed as a warning.
}