Comments (11)
@Haegin we have similar use cases. Maybe our approach could be useful to you?
Our jobs are extremely thin wrappers around our LS code, so they contain no logic, except for calling the organizer, and then failing the job by throwing an exception if outcome.failure?
In fact - we don't actually have _job.rb
files anymore... We build our organizers that need to be run via a job system like so:
class MyReportOrganizer < ApplicationJob # NOTE: Inherits from ApplicationJob
extend LightService::Organizer # as well as extending ls-org
queue_as :default
def self.call(payload)
with(date_range: payload[:date_range], email: payload[:email])
.reduce(actions)
end
def self.actions
[
Reports::CollectRawData,
Reports::WriteSpreadsheet,
Reports::EmailReport,
]
end
def perform(start_date_string, end_date_string, email)
date_range, email = hydrate(start_date_string, end_date_string, email)
outcome = MyReportOrganizer.call(date_range: date_range, email: email)
raise RuntimeError("Unable to process report.") if outcome.failure?
true
end
def hydrate(start_date_string, end_date_string, email)
start_date = Date.parse(start_date_string)
end_date = Date.parse(end_date_string)
date_range = (start_date..end_date)
[date_range, email]
end
end
So, what we've got here is a hybrid organizer/job that allows us to call it in the traditional manner from a controller, rake task, with rich objects etc and have it run synchronously, or call it like so:
MyReportOrganizer.perform_later(a_bunch, of_primitive, objects_here)
and it will take those primitives (which some job systems require), hydrate them into rich objects that are required for synchronous calling, and then run the organizer, failing it with an exception (to ensure a retry) if the job is a failure?
.
Hope this helps.
from light-service.
@adomokos - sorry man, been really busy... this is still on my todo to add to the wiki
from light-service.
OK - wiki article is finally up: https://github.com/adomokos/light-service/wiki/Running-Light-Service-organizers-and-actions-in-jobs
from light-service.
What jobs are you referring to? Sidekiq? Resque? Maybe Rails?
from light-service.
We're using Delayed::Job via ActiveJob, but I imagine the approach would be the same for any of the above.
from light-service.
We've used LS with Resque to call APIs and persist data similar to how you described it. Resque workers picked up the payload, invoked a LS organizer with that data, and called a series of actions. Would that work for you?
from light-service.
@adomokos - lemme know if you want me to update the readme with this as an advanced usage example...
from light-service.
@gee-forr - that's exactly how I'd do it.
I wouldn't add it to the main README.md, but having an entry with the comment (and code example) you provided in the wiki would be helpful.
from light-service.
I'll close this issue once the entry is created in the wiki.
from light-service.
I'll close it once it's in the wiki.
from light-service.
Thank you so much for adding that page to the Wiki, @gee-forr, closing this issue. 👍
from light-service.
Related Issues (20)
- Is there a LightService-like project in Python? HOT 2
- Yard Documentation to code write with light-service HOT 4
- Deprecation warning using organizer within an organizer HOT 7
- Rollback Block Not Invoked In Iterated Actions HOT 10
- Testing actions in isolation HOT 4
- [RFC] Closing pull requests while a new build is starting causes failures HOT 1
- reusing modules within actions
- Surprising `add_to_context` orchestrator behaviour HOT 1
- How to handle default values for dependency injection? HOT 9
- Help testing LS release candidate HOT 6
- Why this gem add activesupport as runtime dependency? HOT 12
- around for Actions HOT 3
- Namespacing Organizers and Actions HOT 1
- Confused about context failure HOT 1
- Can I chain Organizers together? HOT 5
- `reduce_case` not supported when running Ruby < 2.7, but not documented as such HOT 1
- README's built-in localization adapter documentation has a bug HOT 1
- How about an "after" hook that will be called after "fail_and_return!"? HOT 2
- Remove the ActiveSupport dependency HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from light-service.