Job ID and Proc System
Salt minions maintain a directory for managing all the files. We call it the proc directory. It is present inside the cachedir directory of salt. Each file present in this folder has got a unique Job ID. The primary purpose of these IDs is to identify the jobs uniquely. We can track and access the currently running jobs on the minion.
SaltUtil Module
Salt provides a Saltutil module. This module is responsible for the job management process. It gives us different functions to manage jobs at the minion level. Let us discuss these functions in detail:
-
running: This function returns the data of all the jobs running and present inside the proc folder.
-
find_job: This function is handy and provides data about a particular job. It takes the Job ID parameter as input.
-
signal_job: It allows us to send a specific job ID as a signal.
-
term_job: This function terminates a process controlling the given job. To do this task, it sends a termination signal(SIGNAL, 15).
- kill_job: You can use this function to kill a process controlling the given job. Similar to the term_job, it also uses a signal(SIGKILL, 9) to kill the job.
Jobs Runner
The jobs runner works at the front end and provides a more accessible data view. It has three main functions, which we discuss below:
ACTIVE Function
The main task of this function is to compare the jobs that are still running with the ones that have successfully returned. This function makes it easier to see whether a minion has completed a job. We execute it using the following command:
salt-run jobs.active.
LOOKUP_JID Function
By default, the data that returns to the master after the successful execution of a job gets cached for 24 hours. We can change this duration using the keep_jobs option. The lookup_jid runner displays the same data returned during the initial job execution using the salt command. Take a look at the code below:
salt-run jobs.lookup_jid <job id number>
This function takes the job id as a parameter.
LIST_JOBS Function
This function lists the job data for all completely or partially executed jobs, including the job id. Check out the code below:
salt-run jobs.list_jobs
Job Scheduling
Salt’s scheduling exposes the execution of an executive function on minions or any runner on the master.
We can perform it using the following methods:
-
schedule: This option is either the master or minion configuration files. We have to restart the server application to implement the scheduling.
-
Minion pillar data: It implements the schedule by refreshing the minion’s pillar data. For example, we can use saltutil.refresh_pillar.
- The schedule module or schedule state.
We execute salt states on the minion. We can pass the positional arguments and provide a YAML of the named arguments inside the configuration file.
Schedule syntax:
schedule:
job1:
function: package1.install
seconds: 1800
args:
- httpd
#optional
kwargs:
test: True
splay:
start: 5
end: 20
This code will execute package1.install function with the provided httpd argument every 30 minutes, splaying the time between 5 to 20 seconds. Kwargs and splay are both additional arguments.
Scheduling By Date and Time
We can also schedule the date and time using Python’s dateutil library. We use the when keyword to specify the date and time.
schedule:
job1:
function: package1.install
args:
- httpd
kwargs:
test: True
when:
- Monday 2:30 pm
- Tuesday 3:00 pm
- Friday 11:00 pm
This code will schedule the package1.install function to run on Monday, Tuesday, and Friday at the provided timings.
We can use the once keyword to execute a command only once at a particular time.
once: '2022-02-19T07:14:00'
We can also use the range keyword to specify the start and end time for job execution.
range:
start: 10:00 am
end: 2:00 pm
We can limit the maximum number of jobs that can run parallel to each other using the maxrunning keyword. This limit is set to 1 by default.
schedule:
job1:
function: large_data_transfer
jid_include: True
maxrunning: 4
Return Job and Metadata
By default, the Salt scheduler returns the data about the executed jobs to the master. If you want to prevent it, you can set the return_job parameter to false.
return_job: False
The Job Metadata allows us to include additional specific data, which helps us to identify jobs uniquely. We can associate a job with particular values. However, we must specify the metadata parameter as a dictionary, or else Salt will ignore it.
metadata:
abc: xyz
Run On Start
By default, a job schedule initiates when a minion starts up. Now you may not want this to happen. So, if you wish to disable this feature, you can set the run_on_start parameter to False. This parameter will tell the scheduler to skip the first run and wait for the next scheduled run.
Until and After
Sometimes, you may want to avoid a particular job from running after a specific time. Similarly, you may not want a job to start before a given time. You can achieve this by using the until and after keywords.
schedule:
job1:
function: state1.sls
seconds: 1800
after: '02/19/2022 12:00am'
until: '02/20/2022 12:00am'
args:
- httpd
kwargs:
test: True
Note that the dateutil library must support the format of the time specified in the until and after properties.
Frequently Asked Questions
What do you understand by master and minions in Salt?
Salt worlds on a client-server system. The client servers which execute Salt jobs are called Minions. The minions return the execution result to a master server, known as the Master. This master issue commands to the minions and manages jobs.
Which type of functions are executed by the scheduler on the master and minions?
The scheduler executes various types of functions on the master and minions. When it operates on the minion side, it runs only execution functions. However, if the scheduler operates on the master, it executes runner functions.
How do I increase the maximum number of jobs that can be run parallelly in Salt?
By default, the salt scheduler ensures that the maximum number of parallel jobs is one. However, we can increase this number by changing the maxrunning attribute to a larger value. For example, maxrunning: 4.
Conclusion
In this article, we discussed the Job Management system in Salt. We learned about Job IDs, the Saltutil module, and various job scheduling techniques.
We sincerely hope the above discussion helped you understand Job management in Salt. You can refer to our blogs on job cache and access control if you wish to learn more about Salt.
Visit our website to read more such blogs. Make sure you enroll in our other courses as well. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.
Keep Grinding! 🦾
Happy Coding! 💻