#should this invoke be re-run when restoring a machine from persisted snapshot

1 messages · Page 1 of 1 (latest)

rare zodiac
#

this is how I'm restoring it:

    const { url } = job.data;
    this.logger.log(`Processing job ${job.id} for URL: ${url}`);

    // Capture 'this.logger' to use inside action implementations
    const logger = this.logger;

    // Provide actual implementations to the machine
    const machineWithImplementations = websiteAnalysisMachine.provide({
      actions: {
        logStart: () => {
          logger.log('Starting website analysis');
        },
        logCompletion: () => {
          logger.log('Website analysis completed');
        },
        logError: ({ context }) => {
          logger.error('Error in website analysis:', context.error);
        },
      },
      actors: {
        crawlWebsite: fromCallback(({ sendBack, input }) => {
          (async () => {
            try {
              console.log('we are in here');
              const data = await this.websiteAnalysisService.crawlWebsite(
                input.url,
                input.currentProgress || 0, // Use current progress
                (progress) => {
                  sendBack({ type: 'CRAWL_PROGRESS', progress });
                },
              );
              sendBack({ type: 'CRAWL_COMPLETE', data });
            } catch (error) {
              sendBack({ type: 'FAIL', error: error.message });
            }
          })();
        }),
        analyzeContent: fromPromise(async ({ input }) => {
          try {
            const data = await this.websiteAnalysisService.analyzeContent(
              input.url,
            );
            return data;
          } catch (error) {
            throw error;
          }
        }),
        analyzeBacklinks: fromPromise(async ({ input }) => {
          try {
            const data = await this.websiteAnalysisService.analyzeBacklinks(
              input.url,
            );
            return data;
          } catch (error) {
            throw error;
          }
        }),
        generateSEOReport: fromPromise(async ({ input }) => {
          try {
            const { contentAnalysis, backlinkData } = input;
            const report = await this.websiteAnalysisService.generateSEOReport(
              contentAnalysis,
              backlinkData,
            );
            return report;
          } catch (error) {
            throw error;
          }
        }),
      },
    });

    // Get persisted snapshot if it exists
    const persistedSnapshot = await this.getPersistedSnapshot(job.id);

    const actor = createActor(machineWithImplementations, {
      input: { jobId: job.id, url },
      snapshot: persistedSnapshot,
    });
#

from the docs it seems to say it should be recreating it:

If the state is compatible with the actor logic, when the actor is started it will be at that persisted state. Actions from machine actors will not be re-executed, because they are assumed to have been already executed. However, invocations will be restarted, and spawned actors will be restored recursively.
#

it's an actor not an action so I don't understand why it doesn't restart 😦

rare zodiac
#

is it because of parallel states perhaps?

#

should I perhaps try with Event sourcing instead?

#

so wierd, everything seems to be proper but on restart after restoring from snapshot it just gets stuck and doesn't invoke crawlWebsite again:

[Nest] 452  - 10/13/2024, 10:53:28 AM     LOG [WebsiteAnalysisProcessor] Processing job 20 for URL: https://example.com
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] Actor created for job 20
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] State update for job 20: {"analyzing":{"crawling":"active","contentAnalysis":"complete","backlinkAnalysis":"complete"}}
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] Actor started for job 20
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] Updated progress for job 20: 10% < comment: this progress is updating the bullmq .progress from the loaded snapshot
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] Persisted snapshot for job 20
[Nest] 452  - 10/13/2024, 10:53:28 AM   DEBUG [WebsiteAnalysisProcessor] Published state update for job 20

rare zodiac
rare zodiac
#

any idea why it doesn't re-start?

royal ravine
#

Hey there. Really just guessing, but there is a "reenter" flag that can be set to true. REally no idea how it really works but it sounds vaguely related. Maybe you can read up on it