Service: Mobile Service Management
This service
module provides many interfaces for applications to automatically manage jobs.
Developers can determine whether these interfaces work in the EdgerOS mobile App environment through the following code:
edger.env().then(data => {
if (data.env === 'edgerapp') {
// We work in EdgerOS mobile App environment.
}
}).catch(error => {
console.error(error);
});
Functions
edger.service.create(params)
Create a service that can be used to automatically manage jobs.
params
{Object} Parameters for creating services.- Returns: {Promise} Promise object.
The params
is the service parameter object, it can contain the following members:
name
{String} Custom service name.service
{String} Service type. Currently, only the service typealbums_monitor_service
is supported.event
{String} Service Event. Currently, only the eventcreated
is supported.condition
{Object} Service condition.job
{Object} Job object. For details, please refer to Job: Mobile Job Management.
The condition
attribute in params
is an object, it can contain the following members:
background
{Boolean} Whether to run in the background.filetype
{String} File type. Optional values:video
|livephoto#mov
|livephoto
|picture
|file
|livephoto#thumbnail
|all
.wifiOnly
{Boolean} Iftrue
, jobs are excuted only when wifi is available. Default:false
.
The job
attribute in params
is an object, it can contain the following members:
template
{String} Template name. For details, please refer to edger.job.create(template, info, options).url
{String} Service URL.method
{String} HTTP method.uploadThumbnail
{Boolean} Iftrue
, the thumbnail will be uploaded. Default:false
. Optional.info
{Object} Job information. For details, please refer to edger.job.create(template, info, options).options
{Object} The configuration parameters of job. For details, please refer to edger.job.create(template, info, options).
The returned object can contain the following members:
id
{String} Service ID.status
{String} Service status.service
{Object} Service object.
The service
object in returned object has almost the same structure as the incoming service
object.
Example
const params = {
name: "albums_monitor_service",
service: "albums_monitor_service",
event: "created",
condition: {
background: true,
filetype: "all",
wifiOnly: true
},
job: {
template: "job_transport_upload_form",
url: `${window.location.origin}/api/form`,
method: "POST",
uploadThumbnail: true,
info: {
headers: {
},
params: {
"storagePath": "/",
},
keyAlias: {}
},
options: {
tags: []
},
},
};
edger.service.create(params).then((payload) => {
const { id } = payload;
console.log("service create successful.", id);
}).catch(error => {
console.error(error);
});
async / await
try {
const { id } = await edger.service.create(params);
console.log("service create successful.", id);
} catch (error) {
console.error(error);
}
edger.service.run(params)
Run the service that created before.
params
: {Object} Parameters for running services.- Returns: {Promise} Fulfill with undefined success.
The params
attribute is an object, it can contain the following members:
id
{String} Service ID, service unique identification.files
: {Array} Files lists.
The files
attribute is an array, each item object contains the following members:
filePath
{String} The path of file.fileType
{String} The type of file.fileName
{String} The name of file.uniqueId
{String} The unique ID of file.fileSize
{Number} The size of file.birthtime
{Number} The birth time of file. Optional.uploadThumbnail
{String} Iftrue
, the thumbnail will be uploaded. Default:false
. Optional.
Example
const params = {
id: '000001',
files: [
{
filePath: '/path',
fileType: 'livephoto',
fileName: 'edgeros',
fileSize: 20023,
uniqueId: '12345-67890',
uploadThumbnail: true
}
]
};
edger.service.run(params).then((payload) => {
console.log("service run successful.");
}).catch(error => {
console.error(error);
});
async / await
try {
await edger.service.run(params)
} catch (error) {
console.error(error);
}
edger.service.pause(id)
Pause the running service.
id
{String} Service unique identification.- Returns: {Promise} Promise object.
The returned object can contain the following members:
id
{String} Service ID.status
{String} Service status.
Example
const id = '000001';
edger.service.pause(id).then((payload) => {
console.log("service pause successful.", payload);
}).catch(error => {
console.error(error);
});
async / await
async function pause(id) {
try {
await edger.service.pause(id)
} catch (error) {
console.error(error);
}
}
edger.service.resume(id)
Resume the running service.
id
{String} Service unique identification.- Returns: {Promise} Promise object.
The returned object can contain the following members:
id
{String} Service ID.status
{String} Service status.
Example
const id = '000001';
edger.service.resume(id).then(() => {
console.log('service resume successful.');
}).catch(error => {
console.error(error);
});
async / await
async function resume(id) {
try {
await edger.service.resume(id)
} catch (error) {
console.error(error);
}
}
edger.service.query()
Query the service execution records.
- Returns: {Promise} Promise Array.
The returned object can contain the following members:
id
{String} Service ID.status
{String} Service status. Optional values:sleep
|pause
|running
.reason
{String} Reason for service suspension. Optional.lastJob
{Object} Last job information. For details, please refer to edger.job.get(id).digest
{Object} The summary information of service, it can contain the following members:waitingCount
{Number} Waiting count.successCount
{Number} Success count.errorCount
{Number} Error count.totalCount
{Number} Total count.group
{String} Latest group ID.
The corresponding descriptions of different reason
codes are as follows:
reason | description |
---|---|
done | Backup to the latest |
nowifi | Non-wifi causes pause |
manual | Manual Pause |
uploading | Uploading |
nocontent | No backup content |
nonetwork | No network |
background | Do not allow background backup |
Example
edger.service.query().then((res) => {
const { id, status, reason, lastJob, digest } = res
console.log('service list:', id, status, reason, lastJob, digest);
}).catch(error => {
console.error(error);
});
async / await
async function query() {
try {
const { id, status, reason, lastJob, digest } = await edger.service.query()
console.log('service list:', id, status, reason, lastJob, digest);
} catch (error) {
console.error(error);
}
}
edger.service.remove(ids)
Remove the specified service.
ids
{Array} Multiple service IDs, service unique identification.- Returns: {Promise} Promise array, each item of array is the information of service.
Get an array of service objects, each service object includes:
id
{String} Service ID.status
{String} Service status. Optional values:running
|pause
|sleep
.service
{Object} Service object.
Example
const ids = ['000001', '000002'];
edger.service.remove(ids).then((res) => {
console.log('service remove success', res);
}).catch(error => {
console.error(error);
});
async / await
async function remove(id) {
try {
await edger.service.remove(id)
} catch (error) {
console.error(error);
}
}
edger.service.status(id)
Query the service status.
id
{String} Service ID, service unique identification.- Returns: {Promise} Promise object.
Get an object of service, it can contain the following members:
id
{String} Service ID.status
{String} Service status. Optional values:running
|pause
|sleep
.service
{Object} Service object.
Example
const id = '000001';
edger.service.status(id).then((res) => {
console.log('service status:', res);
}).catch(error => {
console.error(error);
});
async / await
async function status(id) {
try {
await edger.service.status(id)
} catch (error) {
console.error(error);
}
}
Events
The unified event listener provided by Web-SDK:
const listener = (payload) => {
// Event handling...
}
// add listener
edger.service.addEventListener('some-event', listener);
// or
// onAction() is an alias of addEventListener().
edger.service.onAction('some-event', listener);
// remove listener
edger.service.removeEventListener('some-event', listener);
// remove all listeners
edger.service.removeAllListeners();
status
This event will be triggered when the service status changed.
- Returns: {Object} Payload object.
Get an object of result, it can contain the following members:
id
{String} Service ID.status
{String} Service status. Optional values:running
|pause
|sleep
.service
{Object} Service object.
Example
edger.service.addEventListener('status', (payload) => {
const { id, status, service } = payload;
console.log('Service status info:', id, status, service);
});