When creating a new job, it should be easier (default) to create child instead of a new root job
See original GitHub issueWhen creating a new Job
, the simplest way to do this, and the way most people do it by default is with Job()
or SupervisorJob()
. However, in most cases what is actually intended is to create a job that is a child of some context or scope. The current state of things makes it really easy to accidentally create a job that breaks out of the hierarchy, which can lead to leaks of coroutines and exceptions, and broken cancellation.
A simple extension function could improve things:
fun CoroutineScope.createJob(): Job = Job(parent = coroutineContext[Job])
fun CoroutineScope.createSupervisorJob(): SupervisorJob = SupervisorJob(parent = coroutineContext[Job])
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Configuring and Running a Job - Spring
Inheriting from a Parent Job Similar to class inheritance in Java, a “child” Job combines its elements and attributes with the parent's. In...
Read more >Choose when to run jobs - GitLab Docs
To configure a job to be executed only when the pipeline has been scheduled, use the rules keyword. In this example, make world...
Read more >Pipeline as Code - Jenkins
Pipeline as Code describes a set of features that allow Jenkins users to define pipelined job processes with code, stored and versioned in...
Read more >Create and Configure Jobs and Pipelines Using YAML
All YAML files must reside in the .ci-build directory in the root directory of any hosted Git ... if no errors are detected,...
Read more >Constructs - AWS Cloud Development Kit (AWS CDK) v2
New features will be developed for CDK v2 exclusively. ... They also provide convenience methods that make it simpler to work with the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That binding API looks really nice for (1).
My main use case is (2) in your list however. I’m working on a library that uses
Job
s under the hood to scope and cancel concurrent work (effectively a hierarchical worker pool). The work is configured in a tree, and so we create a parallel tree ofJob
s. Multiple coroutines may be started later using that job as the parent. We’re creatingJob
s explicitly, and not as part of an immediatelaunch
or other new coroutine, so I don’t think this binding approach, as described above, wouldn’t solve my use case.There was an attempt to add such helpers in #688. We decided to postpone it because creating a child out of thin air is a bit dangerous. For example, the following code
never completes because
childJob
will never be completed and without a proper debugger it may be really hard to pin down the problem.Though writing something like
SupervisorJob(coroutineContext[Job])
is not the best experience as well. We want to prototype #1406 to address this issue, for example, instead ofwe will provide a flexible “BindJob” (name TBD, just to show the idea):
Bind(mode = Supervising)
knows how to bind itself as a supervising job between a parentscope
and child job (launch
).It would be really helpful if you could elaborate on why you need such extensions because they have multiple use-cases:
launch(createSupervisorJob)
)Understanding the most common patterns will help us a lot in designing the best possible solution in #1406