Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike: Offer simple ORM interface for scheduling a job #1292

Closed
lampwins opened this issue Jan 21, 2022 · 4 comments
Closed

Spike: Offer simple ORM interface for scheduling a job #1292

lampwins opened this issue Jan 21, 2022 · 4 comments
Assignees
Labels
type: feature Introduction of new or enhanced functionality to the application
Milestone

Comments

@lampwins
Copy link
Member

lampwins commented Jan 21, 2022

Environment

  • Python version: 3.8.11
  • Nautobot version: 1.2.4

Proposed Functionality

As P.D. the plugin developer, I want a simple interface to schedule a job execution via the ORM so that I can easily perform this action within the business logic of my plugin.

Use Case

Today it takes ~30 lines of code to create a ScheduledJob instance for a nautobot job. Example:

if job_class.approval_required or schedule_type in JobExecutionType.SCHEDULE_CHOICES:
if schedule_type in JobExecutionType.SCHEDULE_CHOICES:
# Schedule the job instead of running it now
schedule_name = schedule_form.cleaned_data["_schedule_name"]
schedule_datetime = schedule_form.cleaned_data["_schedule_start_time"]
else:
# The job must be approved.
# If the schedule_type is immediate, we still create the task, but mark it for approval
# as a once in the future task with the due date set to the current time. This means
# when approval is granted, the task is immediately due for execution.
schedule_type = JobExecutionType.TYPE_FUTURE
schedule_datetime = datetime.now()
schedule_name = f"{job.name} - {schedule_datetime}"
job_kwargs = {
"data": job_class.serialize_data(job_form.cleaned_data),
"request": copy_safe_request(request),
"user": request.user.pk,
"commit": commit,
"name": job.class_path,
}
scheduled_job = ScheduledJob(
name=schedule_name,
task="nautobot.extras.jobs.scheduled_job_handler",
job_class=job.class_path,
start_time=schedule_datetime,
description=f"Nautobot job {schedule_name} scheduled by {request.user} on {schedule_datetime}",
kwargs=job_kwargs,
interval=schedule_type,
one_off=schedule_type == JobExecutionType.TYPE_FUTURE,
user=request.user,
approval_required=job_class.approval_required,
)
scheduled_job.kwargs["scheduled_job_pk"] = scheduled_job.pk
scheduled_job.save()

In the spirit of JobResult.enqueue_job(), it should not be this hard to schedule a job for execution. We can likely consolidate the JobResult.enqueue_job() method into this too.

Should have this interface live on Job (ex: Job.schedule_job()) with the hope of collapsing JobResult.enqueue_job() onto Job somewhere in the future.

Database Changes

None

External Dependencies

None

TODO:

This should cover interfaces for both scheduled jobs and immediate execution.

@glennmatthews glennmatthews added group: automation type: feature Introduction of new or enhanced functionality to the application labels Jan 24, 2022
@lampwins lampwins added this to the v1.4.0 milestone Apr 8, 2022
@tbotnz
Copy link
Contributor

tbotnz commented Apr 10, 2022

@lampwins/whoever resolves this issue, would you also be able to consider a pattern where JobResult.enqueue_job() is triggered by django signals.
The current arg (user) on JobResult.enqueue_job() makes this pattern (signals triggering jobs) problematic to implement

@lampwins
Copy link
Member Author

lampwins commented Jun 27, 2022

Converting this to a spike with the goal of defining what the interface for a combined enqueue/scheduling method would look like.

@lampwins lampwins changed the title Offer simple ORM interface for scheduling a job Spike: Offer simple ORM interface for scheduling a job Jun 27, 2022
@bryanculver bryanculver modified the milestones: v1.4.0, v1.5.0 Jul 27, 2022
@gsnider2195
Copy link
Contributor

@lampwins/whoever resolves this issue, would you also be able to consider a pattern where JobResult.enqueue_job() is triggered by django signals. The current arg (user) on JobResult.enqueue_job() makes this pattern (signals triggering jobs) problematic to implement

That should be covered by the work for #1631

@bryanculver
Copy link
Member

Design complete. See #2474. To be implemented via #2535

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: feature Introduction of new or enhanced functionality to the application
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants