question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow concurrency that depends on arguments passed to a task

See original GitHub issue

Scenario:

You have an editor component that periodically auto-saves content using a concurrent task. If the task lives on the component then switching away from the page could loose saves.

So you move the task to a service. However this service is not very useful, as if you use something like keepLatest then only one save can run at a single time, and saves for other models can be lost. What you really want is a keepLatest that depends on an argument that is passed to the task.

I.E, if your first parameter to the task is model, you want the keepLatest functionality for each task launched with the same model instance. So you could have two concurrent tasks with different models running, but repeated calls to that task with the same model would trigger a queue or drop if one is already running.

I hope this makes sense, sorry if it doesn’t! I was looking at the source code, couldn’t it be possible to pass in the task instance or the arguments passed to the Scheduler.bufferPolicy.schedule function as a start, then maybe have a policy that accepts a function as it’s first argument, returning a stable key from the given task arguments. Then the policy can apply the current logic to tasks that share that unique key?

In the case of the example above, you could perhaps do:

task(...).keepLatest(model=>`${model.get('constructor.modelName')}-${model.get('id')`)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
gabrielgrantcommented, Apr 2, 2018

Perhaps this is a silly question, but if the task is something performed on the model, is there a reason the task shouldn’t live on the model?

0reactions
c-emilcommented, Oct 30, 2018

@gabrielgrant I completely agree, having this functionality inside the model is not the first approach you’d take in many situations. But if it’s, for example, just about debounced saving to backend, it’s a very good solution. Let’s say you wanna save every change that user makes on a model, but it’s possible that user will change the model few times in few seconds (for example choosing a bunch of checkboxes as answers on a question - which is the model). Then this is great solution because you can debounce the saving.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Task Parallelism (Concurrency Runtime) - Microsoft Learn
In the Concurrency Runtime, a task is a unit of work that performs a specific job and typically runs in parallel with other...
Read more >
CeleryD seems to ignore concurrency argument
I've recently upgraded my Django project to Celery 4.4.6 and things aren't going well. My current number one problem is the concurrency of...
Read more >
Tasks - Prefect 2 - Coordinating the world's dataflows
Tasks allow a great deal of customization via arguments. Examples include retry behavior, names, tags, caching, and more. Tasks accept the following ...
Read more >
Concurrency
Fig.1 - Info flow of task-based concurrency. ... we simply have to declare an obj of class std::thread and pass the desired task...
Read more >
How to create and use task local values - Hacking with Swift
In real-world code, task-local values are useful for places where you need to repeatedly pass values around inside your tasks – values that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found