Skip to content

Commit

Permalink
Merge pull request #36 from sjdonado/fix/workers-free-plan-limitation…
Browse files Browse the repository at this point in the history
…s-workaround

Fix/workers free plan limitations workaround
  • Loading branch information
sjdonado authored Apr 14, 2024
2 parents 262b4f0 + be8a633 commit b3bc196
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
21 changes: 12 additions & 9 deletions app/jobs/search.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export async function startOrCheckSearchJob(context: AppLoadContext, key: string
const encodeMessage = (message: string, percentage: number, time = Date.now()) =>
encoder.encode(`data: ${time},${(percentage * 100).toFixed(1)},${message}\n\n`);

// if job has finished (if has failed to retry is allowed)
if (job.state === SearchJobState.Success) {
// check if job is not running or completed
if ([SearchJobState.Running, SearchJobState.Success].includes(job.state)) {
console.log(
`[${startOrCheckSearchJob.name}] (${key}) finished with status ${job.state} (${job.stage})`
`[${startOrCheckSearchJob.name}] (${key}) already running or completed: ${job.state}`
);

return encodeMessage(DONE_JOB_MESSAGE, 1);
Expand Down Expand Up @@ -213,11 +213,11 @@ export async function startOrCheckSearchJob(context: AppLoadContext, key: string

sendEvent(
`Fetching photos for "${place.displayName.text}"...`,
increaseProgress(progress < 0.6 ? 0.6 - progress : 0)
increaseProgress(0.05)
);

await Promise.all(
(place.photos ?? []).slice(0, 5).map(async (photo, photoIdx) => {
(place.photos ?? []).slice(0, 4).map(async (photo, photoIdx) => {
const binary = await downloadPlacePhoto(context, photo.name);
const caption = await runImageToTextRequest(context, binary);

Expand Down Expand Up @@ -266,10 +266,13 @@ export async function startOrCheckSearchJob(context: AppLoadContext, key: string
})
);

const [placesDescriptions, placesThumbnails] = await Promise.all([
placesDescriptionsPromise,
placesThumbnailsPromise,
]);
// Not supported by workers in the free plan https://developers.cloudflare.com/workers/platform/limits/#simultaneous-open-connections
// const [placesDescriptions, placesThumbnails] = await Promise.all([
// placesDescriptionsPromise,
// placesThumbnailsPromise,
// ]);
const placesDescriptions = await placesDescriptionsPromise;
const placesThumbnails = await placesThumbnailsPromise;

sendEvent(
`Almost done! Parsing results...`,
Expand Down
11 changes: 2 additions & 9 deletions app/routes/_main.history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ export const loader = async ({ context }: LoaderFunctionArgs) => {

const searches = await Promise.all(
records.map(async record => {
// TEMPFIX: safeParse ignores previous results with different schema (it can be removed if KV is purged)
const result = await SearchJobSerializedSchema.safeParseAsync({
id: record.id,
input: record.input,
state: record.state,
places: record.places,
createdAt: record.createdAt,
});
const result = await SearchJobSerializedSchema.safeParseAsync(record);

if (result.success) {
return result.data;
Expand All @@ -29,7 +22,7 @@ export const loader = async ({ context }: LoaderFunctionArgs) => {
);

const sortedSearches = searches
.filter(Boolean) // TEMPFIX: didn't find a way to purge the KV local cache
.filter(Boolean)
.sort((a, b) => new Date(b!.createdAt).getTime() - new Date(a!.createdAt).getTime());

return {
Expand Down
5 changes: 3 additions & 2 deletions app/routes/_main.search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useSearchParams,
} from '@remix-run/react';

import { DONE_JOB_MESSAGE, SearchJobState } from '~/constants/job';
import { DONE_JOB_MESSAGE, SearchJobStage, SearchJobState } from '~/constants/job';

import { SearchSchema } from '~/schemas/search';
import type { SearchJobSerialized } from '~/schemas/job';
Expand Down Expand Up @@ -71,7 +71,8 @@ export default function SearchPage() {

const startSearchJob = useCallback(async () => {
if (
searchJob?.state === SearchJobState.Created ||
(searchJob?.state === SearchJobState.Created &&
searchJob?.stage === SearchJobStage.Initial) ||
(retry && searchJob?.state === SearchJobState.Failure)
) {
const eventSource = new EventSource(`/search/job/${jobId}`);
Expand Down
1 change: 1 addition & 0 deletions app/schemas/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const SearchJobParsedSchema = SearchJobSchema.extend({
export const SearchJobSerializedSchema = SearchJobSchema.pick({
input: true,
state: true,
stage: true,
logs: true,
createdAt: true,
})
Expand Down
16 changes: 8 additions & 8 deletions app/services/cloudflare.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function runLLMRequest(
stream: false,
});

console.log(`[${runLLMRequest.name}] ${JSON.stringify({ prompt, data }, null, 2)}`);
// console.log(`[${runLLMRequest.name}] ${JSON.stringify({ prompt, data }, null, 2)}`);

return (data as { response: string }).response;
}
Expand All @@ -74,13 +74,13 @@ export async function runSummarizationRequest(
max_length: 3072, // the default is 1024
});

console.log(
`[${runSummarizationRequest.name}] ${JSON.stringify(
{ input, output: data.summary },
null,
2
)}`
);
// console.log(
// `[${runSummarizationRequest.name}] ${JSON.stringify(
// { input, output: data.summary },
// null,
// 2
// )}`
// );

return data.summary;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "culefilo",
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"sideEffects": false,
"type": "module",
Expand Down

0 comments on commit b3bc196

Please sign in to comment.