View Source Bildad.Job.JobEngine (Bildad v0.1.7)

This module contains the logic for enqueuing, running, killing and expiring jobs in the Bildad job scheduling framework.

Summary

Functions

Used to mark a job as completed successfully.

Adds a job to the queue. The order it will be run in will be based on priority and resource availability.

Enqueues a job and triggers the job engine to run immediately.

Used when a job is not running on any node and the expiration time has been reached.

Jobs that come to completion and are not successful are marked as failed.

Locates the Elixir process by the job identifier. If the process is not running or is running on another node then nil is returned.

Utility function go get the number of successful jobs and the number of jobs that failed.

The queue entry points to the current run which has its retry count. This function increments that number by one OR it returns 0 if there is not a currently running job (which is the initial state of an enqueued job).

If the Elixir process is running on this node then it kills the process and updates the database record.

Launches the Elixir process for the job passing it the job context. It registers the process with the job identifier so that it can be found later.

Removes a job from the queue. This is typically called when a job is completed or when a user wishes to no longer run the job.

This function is called by the job scheduler to run a job.

This function is called by the job scheduler to run the job engine.

This function is called by the job scheduler to stop a job that is currently running. This ONLY updates the record in the database, it does NOT stop the elixir process. To stop the Elixir process if it is still running then kill_a_job should be called.

Tries to kill the process by sending an exit signal.

Each job has a schema that defines the shape of the data that will be passed to the job module when it is run.

Functions

Link to this function

complete_a_job(job_config, job_run)

View Source

Used to mark a job as completed successfully.

Link to this function

enqueue_and_run_job(job_config, job_template, job_context, opts \\ %{})

View Source

Enqueues a job and runs it immediately.

This should be used sparingly as it bypasses the priority of other jobs in the queue.

Link to this function

enqueue_job(job_config, job_template, job_context, opts \\ %{})

View Source

Adds a job to the queue. The order it will be run in will be based on priority and resource availability.

Each message contains a job_context which is a map of data that will be passed to the job module when it is run. This job context must conform to the schema defined in the job template.

Link to this function

enqueue_job_and_trigger_engine(job_config, job_template, job_context, opts \\ %{})

View Source

Enqueues a job and triggers the job engine to run immediately.

If there are other jobs ahead of this one in the queue that are higher priority, they will be run first.

This is the preferred over enqueue_and_run_job as it respects the priority of other jobs in the queue.

TODO: This should make the same API call that the cron job makes and it should check to see if the job engine is already running before starting it.

Link to this function

expire_a_job(job_config, job_run)

View Source

Used when a job is not running on any node and the expiration time has been reached.

Link to this function

fail_a_job(job_config, job_run, error_message)

View Source

Jobs that come to completion and are not successful are marked as failed.

Link to this function

find_elixir_process(job_run)

View Source

Locates the Elixir process by the job identifier. If the process is not running or is running on another node then nil is returned.

Utility function go get the number of successful jobs and the number of jobs that failed.

Link to this function

get_retry_count(job_config, job_queue_entry)

View Source

The queue entry points to the current run which has its retry count. This function increments that number by one OR it returns 0 if there is not a currently running job (which is the initial state of an enqueued job).

Link to this function

kill_a_job(job_config, job_run)

View Source

If the Elixir process is running on this node then it kills the process and updates the database record.

If not, nothing happens on this node. The other nodes should being runnig this function as well and the process will be stopped on one of them. If the process is not running on any node the job will eventually expire when the expiration time is reached. The expire_a_job function should be called to handle that case. (This is done by the job engine.)

Link to this function

launch_job_process(job_config, job_run)

View Source

Launches the Elixir process for the job passing it the job context. It registers the process with the job identifier so that it can be found later.

Link to this function

remove_job_from_queue(job_config, job_queue_entry)

View Source

Removes a job from the queue. This is typically called when a job is completed or when a user wishes to no longer run the job.

Link to this function

run_a_job(job_config, job_queue_entry)

View Source

This function is called by the job scheduler to run a job.

Link to this function

run_job_engine(job_config)

View Source

This function is called by the job scheduler to run the job engine.

This should be called by a single cron job in your environment so that the engine is run on one node at a time.

THIS FUNCTION MUST NOT BE RUN INSIDE A TRANSACTION. The jobs should run in isolation from each other. The queue record should be marked as 'RUNNING' as quickly as possible.

Link to this function

stop_job_in_queue(job_config, job_queue_entry)

View Source

This function is called by the job scheduler to stop a job that is currently running. This ONLY updates the record in the database, it does NOT stop the elixir process. To stop the Elixir process if it is still running then kill_a_job should be called.

Link to this function

try_to_kill_process(process_pid)

View Source

Tries to kill the process by sending an exit signal.

Link to this function

validate_job_context(job_context_schema, job_context)

View Source

Each job has a schema that defines the shape of the data that will be passed to the job module when it is run.