PerformContext.Items not working as I expected

I have a job consisting of two parts, called in the following manner

var jobId = BackgroundJob.Enqueue(() => DataImportService.Run(null, filepath, path, User.Identity.Name));
BackgroundJob.ContinueWith(jobId, () => DataImportService.Notify(null, User.Identity.Name));

The bodies of which are something like

    [DisableConcurrentExecution(60)]
    public static DataImportSummary Run(PerformContext context, string fileName, string path, string userName)
    {
        DataImportService importService = new DataImportService(new DataImportRepository(), path);
        DataImportSummary result = importService.StartDataImport(fileName, userName);

        if (context != null)
        {
            if (!context.Items.ContainsKey("importId"))
            {
                context.Items.Add("importId", result.Id);
            }
            else
            {
                context.Items["importId"] = result.Id;
            }
        }

        return result;
    }

    [AutomaticRetry(Attempts = 0)]
    [DisableConcurrentExecution(60)]
    public static void Notify(PerformContext context, string userName)
    {
        try
        {
            // Do stuff
        }
        catch (InvalidOperationException ex)
        {
            logger.Debug(ex);
            if (context.Items.ContainsKey("importId"))
            {
                logger.Debug("ImportId: {0}", context.Items["importId"]);
                int importId = (int)context.Items["importId"];
                LogImportException(importId, ex);
            }
        }
    }
}

My expectation was that since I put something in the Items collection in the Run method it would be there in the subsequent call to Notify but this is not the case and the Items collection does not contain a key named “importId”

Any help would be appreciated

Continuation is actually a separate job. The only difference is invocation trigger: it starts on parent job completion instead of running at given time. It has a separate PerformContext and knows nothing about the parent job, thus its Items collection is also different.

Items collection was actually designed for job filters to keep data (e.g. initializing value in OnPerforming and cleanup in OnPerformed callbacks). It is not persisted in the database, and won’t survive server restarts, so it cannot be used to pass data between jobs.

You may either implement your Notify code as a job filter (so it can access Items collection), or save intermediate data somewhere in the database to be accessible from Notify job.

Alexey,

many thanks. That certainly clears things up.

Phil.