1# Work Scheduler 2 3- [Introduction](#section11660541593) 4- [Directory Structure](#section161941989596) 5- [Work Scheduler](#section1312121216216) 6 - [Available APIs](#section114564657874) 7 - [Usage Guidelines](#section129654513264) 8 - [Restrictions on Using Work Scheduler](#section1551164914237) 9 10- [Repositories Involved](#section1371113476307) 11 12## Introduction<a name="section11660541593"></a> 13 14When an application needs to perform tasks that do not require high real-time performance, such as data learning when the device is idle, the work scheduler can be used. When the application setting conditions are met, the mechanism will uniformly decide the scheduling time according to the current state of the system, such as memory, power consumption, heat, etc. 15 16## Directory Structure<a name="section161941989596"></a> 17 18``` 19 20├── frameworks # Frameworks 21├── interfaces 22│ └── kits # External APIs 23├── sa_profile # SA profile 24├── services # Services 25└── utils # Utilities 26 27``` 28## Work Scheduler<a name="section1312121216216"></a> 29 30### Available APIs<a name="section114564657874"></a> 31 321. The members of WorkInfo 33 34 API | Description | Type 35 ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ 36workId | The id of work(required)|number 37bundleName | The bundle name(required)|string 38abilityName | The ability name(required)|string 39networkType | The type of network | NetworkType 40isCharging | Is charging. | bool 41chargerType | The charging type. | ChargingType 42batteryLevel | The battery | number 43batteryStatus| The battery status | BatteryStatus 44storageRequest|The storage status| StorageRequest 45isRepeat|Is repeating work | boolean 46repeatCycleTime | The repeating cycle time| number 47repeatCount | The repeating cycle count| number 48 49 502. The api of work scheduler for starting, stopping and querying. 51 52 API | Description 53 ------------------------------------------------------------ | ------------------------------------------------------------ 54 function startWork(work: WorkInfo): boolean; | work scheduler application 55 function stopWork(work: WorkInfo, needCancel?: boolean): boolean; | work scheduler cancel 56 function getWorkStatus(workId: number, callback: AsyncCallback<WorkInfo>): void; | get the status of work(Callback form) 57 function getWorkStatus(workId: number): Promise<WorkInfo>; | get the status of work(Promise form) 58 function obtainAllWorks(callback: AsyncCallback<void>): Array<WorkInfo>; | get all works(Callback form) 59 function obtainAllWorks(): Promise<Array<WorkInfo>>; | get all works(Promise form) 60 function stopAndClearWorks(): boolean; | stop and clear work 61 function isLastWorkTimeOut(workId: number, callback: AsyncCallback<void>): boolean; | Get whether the last task has timed out(For repeat work. Callback form) 62 function isLastWorkTimeOut(workId: number): Promise<boolean>; | Get whether the last task has timed out(For repeat work. Promise form) 63 643. Callback api of work scheduler 65 66API | Description 67 ------------------------------------------------------------ | ------------------------------------------------------------ 68 function onWorkStart(work: WorkInfo): void; | Callback when work starts. 69 function onWorkStop(work: WorkInfo): void; | Callback when work stops. 70 71### Usage Guidelines<a name="section129654513264"></a> 72 73When an application needs to perform tasks with low real-time performance, work scheduler can be used. When the conditions set by the application are met, the mechanism will make a unified decision and scheduling time based on the current state of the system, such as memory, power consumption, and heat. 74 75#### Restrictions on Using Work Scheduler<a name="section1551164914237"></a> 76 77Adhere to the following constraints and rules when using work scheduler: 78 79- **Timeout**:The longest running time is 120s each time. 80- **Restrict to WorkInfo**: 81 82(1) WorkId, bundleName and abilityName are required. 83 84(2) At least one condition must be set. 85 86(3) Repeatcycletime should be at least 20 minutes. When setting repeatcycletime, you must select one of isrepeat and repeatcount. 87 88## Repositories Involved<a name="section1371113476307"></a> 89 90Resource Schedule subsystem 91 92**work\_scheduler** 93 94notification_ces_standard 95 96appexecfwk_standard 97 98powermgr_battery_manager